Can you get Unity to ignore the second touch?
I have a script that makes an object move towards the mouse/finger position. When I play this on Android, I can freely drag the object around but whenever I use a second finger the object moves to the center point between both fingers.
I was wondering if there was a way to get Unity to disregard the second finger entirely and only allow one finger on the screen at a time?
Answer by jgodfrey · Jun 26, 2016 at 07:05 PM
You can tell how many touches are detected in a given frame by checking the Input.touchCount property. So, you could just wrap your "move" code with something like;
if (Input.touchCount == 1)
{
// Only 1 touch active here, so do 1-touch motion...
}
Answer by ChaosLight · Jun 30, 2017 at 02:54 PM
For handling a single touch; this will help:
void Update ()
{
// ... some other code ....
if (Input.touchCount > 0)
{
switch (Input.GetTouch(0).phase) {
case TouchPhase.Began:
// code to run when touch begins here...
break;
case TouchPhase.Moved:
case TouchPhase.Stationary:
// code to run when touch is being dragged here...
break;
case TouchPhase.Ended:
// code to run when touch is lifted up and finished here...
break;
case TouchPhase.Canceled:
// code to run when touch is interrupted and does not get to run the 'Ended' case here... (jumps from dragging to being canceled by some interruption like a phone call)
break;
}
}
// ... Some other code ...
} // end Update
if you want to handle multiple touches, just change the IF to a FOR loop [ for (int i = 0; i < Input.touchCount; i++) ] and instead of directly using getTouch(0) use getTouch (i).
Hope its useful.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083720im_/https://answers.unity.com/themes/thub/images/avi.jpg)