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

Touch input left and right

Discussion in 'Scripting' started by jessee03, Dec 30, 2014.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    So I've been trying to figure this all out in multiple different ways but with no success. What I'm trying to do is make it so if your finger is always on the screen, then begins to move left or right it will call the code accordingly. Which in my case the current and previous touch locations as you move should be recorded. I've done some debugging and the delta position always remains near zero while the current position is always way higher. Is there an easy way to keep track of the position that your finger just moved from and the new position?

    Code (csharp):
    1.  
    2.     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    3.        
    4.                 touch1 = Input.GetTouch(0).position;
    5.                 touch2 = Input.GetTouch(0).deltaPosition;
    6.  
    7.                 if(touch1.x > touch2.x) {
    8.            
    9.                     //move right
    10.            
    11.                 }
    12.                 else if(touch1.x < touch2.x) {
    13.            
    14.                     //move left
    15.            
    16.                 }
    17.             }
    18.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Deltaposition is the change in the position, not the previous position. If you move from '100' to '101' the delta is 1, not 100. Previous position would be current plus (or maybe minus? Whichever) the delta.

    I think you can just check if the deltaposition.x is greatthan or lessthan 0
     
    Hozgen90 likes this.
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    That makes a lot more sense. I will try that out and let you know

    Edit. Worked perfect! Thank you
     
    Last edited: Jan 1, 2015