Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

My Button Script isn't working with multitouch?!?!?!?!

Discussion in 'Scripting' started by markzareal, Dec 5, 2014.

  1. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Code (JavaScript):
    1. function Update () {
    2.     hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    3.         for(var i : int = 0; i < Input.touchCount; i++) {
    4.         var touch : Touch = Input.GetTouch(i);
    5.         if(Input.GetTouch(i).phase == TouchPhase.Began){
    6.            
    7.             if( hit != null &&  hit.collider.gameObject.name == "Right"){
    8.                 Right.GetComponent(SpriteRenderer).sprite = afterRight;
    9.                 bool = true;
    10.             }
    11.  
    12.             if( hit != null && hit.collider.gameObject.name == "Left"){
    13.                   Left.GetComponent(SpriteRenderer).sprite = afterLeft;
    14.                   bool2 = true;
    15.             }
    16.         }
    17.         }
    18.            
    19. }
    for some reason this code isn't working with multitouch. Its a code for the arrow buttons to control the character. But I want to be able to touch 2 buttons at a time- for example to touch both the right button and the jump button. But it would only allow me to touch one. Whats wrong with my code?!?! Btw this code is attached to the camera.
     
  2. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    Your "hit" variable is defined based on the position of the mouse. For touch applications, Unity simulates the mouse position as the same as the first touch position, but for multitouch applications you'll need to perform a new raycast for each touch (personally, I don't like using Input.mousePosition even for single touch applications - it's undocumented and I'd rather explicitly code using Touch.position).
     
  3. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Can you give me an example how to do that?
     
  4. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297