Unity:具有两个不同脚本的多点触控

时间:2013-11-04 07:13:00

标签: mobile unity3d touch unityscript

我试图在我的游戏中允许多点触控..我有两个脚本,一个处理相机旋转,另一个处理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");
       }
*/       }
       }

}
}

相机旋转脚本(此外,下面的脚本仅在您将手指拖到地形上或“坚固”时才起作用。如果您只是空置空间(例如Sky),它就无法工作......任何方法都有当有人触摸屏幕时,它可以在任何地方工作吗?):

#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;
                        }

                      }      

         }





    }
    }
    }

2 个答案:

答案 0 :(得分:1)

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

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

    for(int i = 0; i&lt; Input.touchCount; ++ i)

  2. 现在检查商店你的左手指触摸一个变量,当它击中GUITexture或在texture2d的区域时说左边的indId,并且对右手指做同样的事情说右边的ID。现在,当您检查左操纵杆中的触摸时,检查Input.touches[i].fingerId != rightId和右侧操纵杆的类似情况,检查Input.touches[i].fingerId != leftId

答案 1 :(得分:0)

尝试统一使用courutines。创建其中两个,每次触摸一个。您可以在更新中调用协同程序。更新使所有人都在一个线程中。 Courutines将以多种方式完成此任务。所以你可以提供多种服务。 (链接:http://docs.unity3d.com/Documentation/Manual/Coroutines.html

相关问题