【问题标题】:Unity: Multitouch with two Different ScriptsUnity:使用两种不同脚本的多点触控
【发布时间】:2013-11-04 07:13:00
【问题描述】:

我试图在我的游戏中允许多点触控。我有两个脚本,一个处理相机旋转,另一个处理 GUITexture 点击。问题是,当有人移动相机时,他们无法同时推动 GUITexture(发射子弹)。

我尝试了多种方法来合并这两个脚本,但我没有让它工作。我找到了一些方法来做实际的多点触控,我只是不知道如何将它合并到我的游戏中(因为我使用多个脚本).. 这是多点触控信息的链接:http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html

谢谢!!

编辑(这里是脚本):

触发按钮脚本:

#pragma strict
//Fire Button Vars
var myImg : GUITexture;
 var touches = Input.touches;
var newBullet : Rigidbody;
var throwSpeed : float = 30.0;

function Start () {

}

function Update () {

var tapCount = Input.touchCount;
if(tapCount > 1) {
     var touch1 = Input.GetTouch(0);
     var touch2 = Input.GetTouch(1);

     //Fire Button
     if (myImg.HitTest(Input.GetTouch(1).position))
    {
       if(Input.GetTouch(1).phase==TouchPhase.Began)
       {
         print("Touch has began on image");
         var newBullet : Rigidbody = Instantiate(newBullet, transform.position, transform.rotation);
newBullet.velocity = transform.forward * throwSpeed;
       }
       if(Input.GetTouch(1).phase==TouchPhase.Stationary)
       {
         print("Touch is on image");
       }
       if(Input.GetTouch(1).phase==TouchPhase.Moved)
       {
         print("Touch is moving on image");
       }
       if(Input.GetTouch(1).phase==TouchPhase.Ended)
       {
         print("Touch has been ended on image");
       }else{
//            if (myImg.HitTest(Input.GetTouch(1).position))
//    {
//       if(Input.GetTouch(1).phase==TouchPhase.Began)
/*       {
         print("Touch has began on image");
         var newBullet2 : Rigidbody = Instantiate(newBullet, transform.position, transform.rotation);
newBullet.velocity = transform.forward * throwSpeed;
       }
       if(Input.GetTouch(0).phase==TouchPhase.Stationary)
       {
         print("Touch is on image");
       }
       if(Input.GetTouch(0).phase==TouchPhase.Moved)
       {
         print("Touch is moving on image");
       }
       if(Input.GetTouch(0).phase==TouchPhase.Ended)
       {
         print("Touch has been ended on image");
       }
*/       }
       }

}
}

相机旋转脚本(另外,下面的脚本仅在您将手指拖到地形或“实体”上时才有效。如果您只是清空空间(例如天空),则它不起作用......任何方法都有当有人触摸屏幕时它可以在任何地方工作?):

#pragma strict

var targetItem : GameObject;
var GUICamera : Camera;
var ambient : GameObject;
var max = 90;
var min = 270;

/********Rotation Variables*********/
var rotationRate : float = 1.0;
private var wasRotating;

/************Scrolling inertia variables************/
private var scrollPosition : Vector2 = Vector2.zero;
private var scrollVelocity : float = 0;
private var timeTouchPhaseEnded: float;
private var inertiaDuration : float = 0.5f;

private var itemInertiaDuration : float = 1.0f;
private var itemTimeTouchPhaseEnded: float;
private var rotateVelocityX : float = 0;
private var rotateVelocityY : float = 0;


var hit: RaycastHit;

private var layerMask = (1 <<  8) | (1 << 2);
//private var layerMask = (1 <<  0);


function Start()
{
    layerMask =~ layerMask;    
}

function FixedUpdate()
{

    if (Input.touchCount > 0) 
    {   //  If there are touches...
         var theTouch : Touch = Input.GetTouch(0);       //    Cache Touch (0)

         var ray = Camera.main.ScreenPointToRay(theTouch.position);
         var GUIRayq = GUICamera.ScreenPointToRay(theTouch.position);


            if(Physics.Raycast(ray,hit,50,layerMask))
            { 

                if(Input.touchCount == 1)
                 {

                   if (theTouch.phase == TouchPhase.Began) 
                      {
                        wasRotating = false; 
                      }     

                      if (theTouch.phase == TouchPhase.Moved) 
                      {

                        targetItem.transform.Rotate(0, theTouch.deltaPosition.x * rotationRate,0,Space.World);
                        wasRotating = true;

                        var angle = theTouch.deltaPosition.x * rotationRate;

                        if (angle > max){
                            angle = max;
                        }
                        else if (angle < min){
                            angle = min;
                        }

                      }      

         }





    }
    }
    }

【问题讨论】:

  • “我有两个脚本”:您应该在帖子中包含这些脚本。
  • 尝试发布您的代码以更好地了解您到底想要什么...
  • 对不起,我已经添加了两个脚本。

标签: mobile unity3d touch unityscript


【解决方案1】:

这里有一个解决方案。您需要处理屏幕上的所有触摸。之后,您需要单独处理那些需要的触摸。

  1. 使用 for 循环处理所有的触摸

    for(int i=0;i

  2. 现在检查将您的左手指触摸存储到一个变量 leftId,当它碰到 GUITexture 或在 texture2d 区域时,对右手指 ID 说 rightId 执行相同的操作。现在,当您检查左侧操纵杆的触摸时,检查 Input.touches[i].fingerId != rightId 和类似的右侧操纵杆检查 Input.touches[i].fingerId != leftId

【讨论】:

  • 感谢您的回复!一个简单的问题,for 循环里面到底有什么?我是否对每个文件中的两个脚本都执行 for 循环?
【解决方案2】:

尝试统一使用 courutines。创建其中两个,每次触摸一个。您可以在更新中调用协程。更新使所有内容集中在一个线程中。 Curutines 将多次执行此操作。因此,您可以进行多次触摸。 (链接:http://docs.unity3d.com/Documentation/Manual/Coroutines.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多