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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android multitouch issue.

Discussion in 'Scripting' started by raibow, Jun 1, 2015.

  1. raibow

    raibow

    Joined:
    Apr 29, 2014
    Posts:
    4
    Hello everyone,

    I am currently working on simple input gesture script. One of the features is zoom in and zoom out (pinch gesture).
    I have the following script based on several examples found on the Internet:

    Code (CSharp):
    1. void Update () {
    2.         if (Input.touchCount == 2)
    3.         {
    4.             if(Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase ==      TouchPhase.Moved)  
    5.             {
    6.                 curDist = Input.GetTouch(0).position - Input.GetTouch(1).position;
    7.                 prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition));
    8.                 touchDelta = curDist.magnitude - prevDist.magnitude;
    9.                 speedTouch0 = Input.GetTouch(0).deltaPosition.magnitude / Input.GetTouch(0).deltaTime;
    10.                 speedTouch1 = Input.GetTouch(1).deltaPosition.magnitude / Input.GetTouch(1).deltaTime;
    11.              
    12.              
    13.                 if ((touchDelta + varianceInDistances <= -10) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
    14.                 {
    15.                     StartCoroutine("ZoomIn");
    16.                 }
    17.              
    18.                 if ((touchDelta +varianceInDistances > 10) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
    19.                 {
    20.                     StartCoroutine("ZoomOut");
    21.                 }
    22.             }
    23.          
    24.         }
    25. }
    While I am debugging an app via Unity Remote 4 everything works perfect. The problems start when I build the application and then install it on the smartphone. After run I tried to check it out but nothing happens. This script does not work. I have checked does multitouch work properly - I have added a Text displays an information about touchCount so the condition:

    Code (CSharp):
    1. Input.touchCount == 2
    should return true. Could anyone help with this stuff? I would be really thankful...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    I'll assume you have Coroutines named ZoomIn and ZoomOut somewhere and that those routines (if called explicitly from a test case) work properly.

    The code you have above is a bit fiddly by requiring that both touch0 and touch1 need to be moving. I would make it so that at least one of them must be moving, and the other may be moving or stationary. That is what a pinch gesture usually can be: one finger fixed, the other moving relative.
     
  3. raibow

    raibow

    Joined:
    Apr 29, 2014
    Posts:
    4
    Thanks for posting and sorry for my delay...

    Yes, those Coroutines work perefectly in Unity Remote. I mean my smartphone gets multitouch data properly but after real app installation 'zoom in' and 'zoom out' gestures don't work. I have also rotation gestures like:
    Code (CSharp):
    1. if(Input.touchCount == 1)
    2.         {
    3.             Touch myTouch = Input.touches[0];
    4.             if(myTouch.phase == TouchPhase.Began)
    5.             {
    6.                 touchOrigin = myTouch.position;
    7.             }
    8.             else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
    9.             {
    10.                 Vector2 touchEnd = myTouch.position;
    11.                 float x = touchEnd.x - touchOrigin.x;
    12.                 float y = touchEnd.y - touchOrigin.y;
    13.  
    14.                 if(Mathf.Abs(x) > Mathf.Abs(y))
    15.                 {
    16.                     if(x > 100)
    17.                         StartCoroutine("RotateLeft");
    18.                     else if(x < -100)
    19.                         StartCoroutine("RotateRight");
    20.                     touchOrigin = touchEnd;
    21.                 }
    22.             }
    23.         }    
    but those work fine in both cases. I have problem only with zoom gestures...
    Good point, thanks. I considered it and fixed my script :) unsuccesfully, zooms still aren't working... Do you have any ideas?
     
  4. raibow

    raibow

    Joined:
    Apr 29, 2014
    Posts:
    4
    Bump, any suggestions ?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Everything looks fine from what I see above. At this point you're probably going to have to write a little logger that outputs each touch gesture to the screen in a scrolling list... pretty sure someone made one of those somewhere on the Unity wiki or something. Just display x, y and touch phase for each touch that exists in any given frame and you should begin to see where the problem might be.