Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trouble getting swipe distance with touch

Discussion in 'Editor & General Support' started by dmennenoh, Oct 4, 2019.

  1. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    I have this simple script:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.             //press down
    4.             if (Input.GetMouseButtonDown(0))
    5.             {
    6.                 currentPosition = Input.mousePosition;
    7.                 Debug.Log("touch down: " + currentPosition.x);
    8.             }
    9.  
    10.             //still down
    11.             if (Input.GetMouseButton(0))
    12.             {              
    13.                 swipeDistance = (Input.mousePosition.x - currentPosition.x) * .75f;
    14.                 currentPosition = Input.mousePosition;
    15.                 Debug.Log("touch still down - swipe dist: " + swipeDistance);
    16.             }
    17.  
    18.             //up
    19.             if (Input.GetMouseButtonUp(0))
    20.             {                        
    21.                 Debug.Log("touch up - swipe: " + swipeDistance);
    22.             }      
    23.     }
    I want the last swipe distance once the user releases their finger - works spot on if I use the mouse. But if I use my touch screen, I get the distance while swiping (when still down), but as soon as I lift my finger I get 0... I just cannot get anything but 0 while touching. Seems like it's a timing thing, but just not sure why mouse works and not touch. Maybe I only update swipeDistance ever 500ms or something?