- Home /
two finger touch always detects one finger touch first
I have two if statements in my update loop. One for a single finger touch and another for a two finger touch. With one finger I am placing and dragging a gameobject. Then with two fingers in a pinch motion, I am scaling the gameobject. The problem is that when I try to touch the screen with two fingers, I usually trigger the one finger function first, causing me to accidentally place the gameobject before being able to scale it.
This script handles the single finger touch. I have a separate script that handles the two finger touch actions.
The only way to prevent this is to touch the screen with both fingers at the exact same time. How can I add some room for error so that when trying to touch the screen with two fingers, even if one finger touches the screen slightly before the other, the one finger action isn't executed? Thank you.
if (Input.touchCount > 1 && m_HitTransform != null)
{
twoFingers = true;
}
else if (Input.touchCount == 1 && m_HitTransform != null && twoFingers == false)
{
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(0))
{
//code to place and drag gameobject
}
}
if (Input.touchCount == 0 && m_HitTransform != null) //no touches
{
twoFingers = false;
}
}
Answer by Acazied · Apr 28, 2020 at 12:37 AM
yes this is how unity touch detecting works . so i made a little hack, you can create a short time interval if the player taps with two fingers before the time interval then start the twofingertap function else start the one finger tap. here is the script
bool OneFingerTap;
float TimerOne;
float timerInterval = 0.1f; //the more you decrease it the more accurate it gets
void Update(){
if(OneFingerTap && (TimerOne + timerInterval <= Time.time)){
HandleOneTapMovement();
OneFingerTap = false;
}
//if there are any touches currently
if(Input.touchCount > 0)
{
// get the first one
Touch firstTouch = Input.GetTouch(0);
//two touches detected
if(Input.touchCount == 2){
Touch secondTouch = Input.GetTouch(1);
if (firstTouch.phase == TouchPhase.Began || secondTouch.phase == TouchPhase.Began){
HandleTwoFingerMovement();
OneFingerTap = false;
}
}
//if Only one touch detected
else (firstTouch.phase == TouchPhase.Began)
{
TimerOne =Time.time;
OneFingerTap = true;
}
}
}
HandleOneTapMovement(){
//one tap function
}
HandleTwoFingerMovement(){
//two tap function
}
Your answer
Follow this Question
Related Questions
Unity Touch Help! 0 Answers
check touch position 2 Answers
How could I implement diagonal movement in this code? 0 Answers
A touch'es state is always Began and the position doesn't change 1 Answer
How to show and hide an image by swiping 0 Answers