1
\$\begingroup\$

How can I detect a long press on a touch screen then activate an animation? And when the finger is released, play another animation smoothly? Let's take for example the main character in Crossy Road: when the user presses down, the chicken "squats" and when the user releases their finger, the chicken jumps. Same concept. C#.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You save the time when the screen is first touched, reset when it's released and each frame when the screen is still touched you check how long has it been since the saved time. \$\endgroup\$
    – AturSams
    Aug 22, 2015 at 10:17
  • \$\begingroup\$ You can view this documentary it should help you with many functions of touch. \$\endgroup\$
    – John Smith
    Aug 22, 2015 at 10:48

3 Answers 3

1
\$\begingroup\$

You can use Input functions to detect these actions. Following code shows how you can achieve it and I have commented the events when they are true.

void Update() 
{
    if (Input.GetMouseButtonDown (0))
    {
        //Touch Begin - True when the finger touches the screen
        //Play animation for chicken squat
    }
    else if(Input.GetMouseButton (0))
    {
        //Touch Continued - True when the finger is still touching the screen
    }
    else if(Input.GetMouseButtonUp (0))
    {
        //Touch End - True when the finger is lifted from the screen
        //Play animation for chicken jump
    }
}

Note: You need to handle a situation where the user touches the menu buttons . Which should not be considered for playing the animation. So for that you need to check your mouse position, whether it is inside a particular area and then only consider the touch for animation.

\$\endgroup\$
1
\$\begingroup\$

You can Detect a long press by simply using Touch.deltaTime.

Unity Document Reference : https://docs.unity3d.com/ScriptReference/Touch-deltaTime.html

    Touch touch = Input.touches;
    for (int i = 0; i < Input.touchCount; i++)
    {
        Touch t = touch[i];
        if (t.deltaTime > 0.2f) // if long touch 
        {
            // do stuff. 

            // If you want to detect long press on an object, do the following.
            Vector3 pos = Camera.main.ScreenToWorldPoint(t.position);

            if (Physics.Raycast(pos, Camera.main.transform.forward, out hit))
            {
               Debug.Log("Long Pressed on "+hit.transform.name);
            }    

        }
     } 
\$\endgroup\$
3
  • \$\begingroup\$ not working, long press never goes in \$\endgroup\$
    – flankechen
    Oct 17, 2019 at 9:18
  • \$\begingroup\$ Can you be more specific, what use case it fails for, what your set up is etc \$\endgroup\$ Oct 19, 2019 at 4:15
  • \$\begingroup\$ in android phone, touch.deltime is always somethong like 0.02-0.03 seconds. \$\endgroup\$
    – flankechen
    Oct 19, 2019 at 4:36
0
\$\begingroup\$

You can define an integer value to use as counter, meaning how long the user has kept his finger on the screen to track a long press:

int timer = 0;

Then, you're going to detect just an event - the mouse pressed event - and increment the timer at your will:

void Update() 
{
    // Increment the timer
    if (Input.GetMouseButton (0))
    {
        timer += 1;
        // Additional code for chicken squat
    }
    else if (timer > 0)
    {
        timer = 0;
        // Additional code for chicken jump
    }
}

The lower the variables to check for, the more performant your code will be.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .