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

How to detect time of time? Long press, or tap?

Discussion in 'Android' started by 1337GameDev, Dec 19, 2011.

  1. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    How do I determine the length of a touch (time) to see if it is a long press, or a tap?

    My code is as follows:

    Code (csharp):
    1.  
    2. if ((Input.touchCount == 1)  (Input.GetTouch(0).phase == TouchPhase.Ended)  (Input.GetTouch(0).deltaTime < (0.5 * Time.deltaTime)))
    3.  
    (First it checks for one touch, then for person to lift finger before firing events, and then to see length of time of touch)

    This doesn't seem to work, how would i change this to work?
     
  2. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Documentation is your friend.

    http://unity3d.com/support/documentation/ScriptReference/Touch.html

    deltaPosition: The position delta since last change.
    deltaTime: Amount of time passed since last change.

    It says since last change, so it can mean when the finger is moved or when the phase was changed.

    You could of course add a variable to store the time when it was first touched and on release check if "(Time.time - timeOfTouch) > 0.5f"

    Edit:
    And in this context the "< (0.5 * Time.deltaTime)" part doesn't make any sense. Time.deltaTime is the time it took to render the last frame. It's basically only used in movement/animation to make it framerate independent
     
  3. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I assumed i would have to make my time independant of frames, as i thought delta time was time since last frame..... (and if frame was low or higher i would get dif times needed for long press).

    If i use the delta time of the touch doesnt it get erratic because your finger is never really "not moving" just moving slightly?
    I'snt deltaTime a number that goes form 0 to 1, as it shows time since last change in terms of one second?
    If i have a touch, and timeDelta starts out at zero for begin phase, then when i release, wont it reset back to zero as the phase changed? Or will the touch object cease to exist and then i cannot access that member?

    Would i just have the code line:

    if ((Input.touchCount == 1) (Input.GetTouch(0).phase == TouchPhase.Ended) (Input.GetTouch(0).deltaTime < 0.5 ))


    if i want to make my touch length smaller than 0.5 secs?
     
  4. mtdrume

    mtdrume

    Joined:
    Jan 28, 2015
    Posts:
    5
    At start of touch event store a float with Time.time;

    At end of touch event subtract current Time.time from stored value and get the difference in time.

    Touch touch = Input.touches [0];

    if (touch.phase == TouchPhase.Began) {
    TouchTime = Time.time;
    }

    if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    {

    if (Time.time - TouchTime <= 0.5)
    {

    //do stuff as a tap
    }
    else
    {

    // this is a long press or drag
    }
    }
     
  5. Arvin6

    Arvin6

    Joined:
    Jun 2, 2017
    Posts:
    27
    This won't work inside update() since Time.time - TouchTime will always be 0.
     
  6. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I don't believe it will be 0.

    This part captures the time the touch began (and stored it in TouchTime):

    Code (CSharp):
    1. if (touch.phase == TouchPhase.Began) {
    2. TouchTime = Time.time;
    3. }
    We want to know the time that has elapsed since the touch began, when it is cancelled or ended, so we subtract the prior time we recorded FROM the current time (and this will always be positive as time increases in the game).
     
  7. Tarun_MH

    Tarun_MH

    Joined:
    Jun 19, 2018
    Posts:
    1
    This Did not work for me