Aubtin Samai
Aubtin Samai

Reputation: 1361

Unity: Multitouch with two Different Scripts

I am trying to allow multitouch in my game.. I have two scripts, one that handles the camera rotation, and another that handles the GUITexture clicks. The issue is, when someone is moving the camera, they are not able to also push the GUITexture (to fire the bullet).

I have tried multiple methods of merging the two scripts, but I am not getting it to work.. I have found some methods to do the actual multitouch, I am just not sure how to incorporate it into my game (since I use multiple scripts).. Here is the link to the multitouch info: http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html

Thanks!!

EDIT (Here are the scripts):

Fire Button Script:

#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");
       }
*/       }
       }

}
}

Camera rotate script (Also, the script below only works when you drag your finger on the terrain or something "solid". It doesn't work if you just empty space (ex. Sky)... Any ways to have it works anywhere when someone touches the screen?):

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

                      }      

         }





    }
    }
    }

Upvotes: 0

Views: 4434

Answers (2)

Nick
Nick

Reputation: 1035

Here there is a solution to this. You need to deal with all the touches that are on the screen. after that you need to just process those touch separately which is in need.

  1. deal with all the touches by using for loop

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

  2. now check the store your left fingerId of touch to a variable say leftId when it hits GUITexture or in the area of texture2d and do the same for right fingerId say rightId. Now when you check for the touch in left joystick check that Input.touches[i].fingerId != rightId and similar for the right joystick check that Input.touches[i].fingerId != leftId

Upvotes: 1

user1600401
user1600401

Reputation: 37

Try to use courutines in unity. Create two of the them, per one for each touch. You may call coroutines in update. Update makes all in one thread. Courutines will do this in multiple. So you able to serve multiple touches. (link : http://docs.unity3d.com/Documentation/Manual/Coroutines.html)

Upvotes: 0

Related Questions