Hey, I wrote a simple code to distinguish between a short and a long tap. Therefore I’m using TouchPhase.Began
to set the start time of the touch and TouchPhase.Ended
to determine the length of the touch. If the length is bigger than a defined threshold, it’s a long tap. Here my code.
float time=0;
const float TouchPhaseThreshold=1.0f;
void Update()
{
if (LongTap() != null)
{
if ((bool)LongTap())
{
Debug.Log("It's a long tap");
}
else
{
Debug.Log("It's a short tap");
}
}
}
private bool? LongTap()
{
if (Input.touchCount == 1)
{
Touch thisTouch = Input.GetTouch(0);
if (thisTouch.phase == TouchPhase.Began)
{
time = Time.time;
return null;
}
if (thisTouch.phase == TouchPhase.Ended)
{
if ((Time.time - time) > TouchPhaseThreshold)
{
return true;
}
else
{
return false;
}
}
}
return null;
}
Unfortunately, regardless of how short i tap on my tablet in Unity remote, it’s always recognized as a long tap by unity. The reason for that is that the condition thisTouch.phase == TouchPhase.Began
is never true, hence time
remains zero and the difference in time is always bigger than the threshold.
Any idea why there is no TouchPhase.Began state here?