- Home /
Double touch on mobile, simple way?
Hi,
i have some issues to understand how we can get a double tap on mobile (android). The code from the complete project of Penelope works fine, but it is more about a "long touch" rather than a double touch. I have tried the code from this thread (http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html), but it does not work :
var count = Input.touchCount;
/* 1/ DOES NOT WORK:
var tt : Touch;
var tcount = tt.tapCount;
if (tcount==1) Debug.Log("______!!!!!!!!!!!!!!!______________");
else if (tcount==2) animation.CrossFade("newAnim");*/
/* 2/ ONLY SAYS count == 1, never count == 2*/
if (count!=0){
Debug.Log("________ count EQUALS :::::: _________"+count);
var tapCount = Input.touchCount;
if(tapCount > 1) {
animation.CrossFade("newAnim");
}
for(var i : int = 0;i < count; i++)
{
if (i==0) Debug.Log("________ i EQUALS ZERO_________");//always shows this one
else if (i==1) Debug.Log("________ i EQUALS OOONNNNEEE_________");//never this one
}
}
I tried to modify the code from "Penelope", but i guess i have to be very careful with the time elapsed between 2 touches :
if (count!=0){
for(var i : int = 0;i < count; i++)
{
Debug.Log("__ME : _____i : "+i);
// Adjust the tap time window while it still available
if ( tapTimeWindow > 0 )
tapTimeWindow -= Time.deltaTime;
else
tapCount = 0;
// Accumulate taps if it is within the time window
if ( tapTimeWindow > 0 )
tapCount++;
else
{
tapCount = 1;
tapTimeWindow = tapTimeDelta;
}
}
if (tapCount==2){
Debug.Log("__ME : _____if tapCount==2 YES");
animation.CrossFade("newAnim");
}
}
Would anyone know why the first code does not work? And if there is a simple solution to get the double touch, instead of calculating the time between 2 touches?
EDIT:
I have tried this one, but i never get more that "one" touch, so it stays at "GetTouch(0)" even when i double tap on the screen :
var count = Input.touchCount;
if (count!=0){
for(var i : int = 0;i < count; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began) Debug.Log("______GetTouch("+i+") !!!______________");
}
}
Thanks
Answer by Eric5h5 · Nov 08, 2012 at 06:29 PM
for (var touch in Input.touches) {
if (touch.tapCount == 2) {
// double-tap
}
}
@Eric5h5 Argh thanks a lot, why my first attempt with the "for loop" did not work? Also, i have read that with this way (var touch in Input.touches) it uses the heap and not only the stack for the memory. Would it be a problem if i use a lot of different touches during my game?
Thanks again Eric!
You could check touchCount and loop through them using Input.GetTouch().
@Eric5h5 how would you do that? Because `if (Input.GetTouch(1))` does not work, as "it cannot be used in a boolean context", and `Input.touchCount == 1` for example does not trigger the Debug.Log ?
@Eric5h5 do you mean adding a TouchPhase value to the getTouch? i edited my post to show you the code, because it does not work for a double tap, only for one touch
Your answer
![](https://koobas.hobune.stream/wayback/20220613083544im_/https://answers.unity.com/themes/thub/images/avi.jpg)