- Home /
Is there a way for my touch input to only detect the most recent touch?
So both inputs work when they are done individually, but if both are pressed simultaneously then they cancel each other out. I would like if only the most recent touch would work so that they don't cancel out. Here is my code:
private void Start () {
rb = GetComponent<Rigidbody2D>();
moveSpeed = 11f;
verticleSpeed = -15f;
}
private void Update () {
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
if (touch.position.x < Screen.width / 2)
rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
if (touch.position.x > Screen.width / 2)
rb.velocity = new Vector2(moveSpeed, verticleSpeed);
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
break;
}
}
Answer by Bunny83 · Jul 09, 2019 at 06:42 PM
This is a common error when dealing with touches. Each touch has a fingerId which actually identifies a finger from the moment it got pressed down (TouchPhase.Began) until the touch ended (TouchPhase.Ended or TouchPhase.Canceled).
If you only want to react to the latest touch you have to iterate through all touches and store the fingerID of the last "began" event. When you get a touch "Ended" you only set speed to 0 when the right last finger is released.
int lastFingerId = -1;
private void Update ()
{
foreach (Touch touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
if (touch.position.x < Screen.width / 2)
rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
else
rb.velocity = new Vector2(moveSpeed, verticleSpeed);
lastFingerId = touch.fingerId;
break;
case TouchPhase.Ended:
if (touch.fingerId == lastFingerId)
rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
break;
}
}
}
This way releasing the second last finger will not have any effect.
Answer by BeanBugDev · Jul 09, 2019 at 07:58 PM
Can't you just.. Touch touch = Input.GetTouch(Input.touchCount - 1)
No because the touches do not necessary have a particular order. In fact on my Nexus 7 the touches actually seem to be ordered by the finger id. Though you have finger 0 and 1 and release finger 0, the finger 1 will be at index 0 since it's the only touch still active. However if you place down another finger, it will get the finger id 0 again and therefore be at index 0 (at least on my device). I've created a simple Unity app long time ago which just visualizes all touches and displays all the information for each touch
Your answer
![](https://koobas.hobune.stream/wayback/20220612204614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
please please please very urgent, can someone convert this script for android swipe 0 Answers
How to make Touch Input act like GetAxis? 0 Answers
convert script to mobile input 0 Answers
How to make a flawless rhythm-like mechanism in mobile 2D? 0 Answers
How do I detect touch on another object and check by tag/name? 1 Answer