How to detect Double Tap in Android

Hello All

I need some code to detect double tap?

bellow function works well:

public static bool IsDoubleTap(){
		bool result = false;
		float MaxTimeWait = 1;
		float VariancePosition = 1;

		if( Input.touchCount == 1  && Input.GetTouch(0).phase == TouchPhase.Began)
		{
			float DeltaTime = Input.GetTouch (0).deltaTime;
			float DeltaPositionLenght=Input.GetTouch (0).deltaPosition.magnitude;

			if ( DeltaTime> 0 && DeltaTime < MaxTimeWait && DeltaPositionLenght < VariancePosition)
				result = true;				
		}
		return result;
	}

I know this post i dead. But for the people still looking for an awnser, this is my shot at it. Im am still a beginner so gime me some feedback.

public class JumpTowardsTap : MonoBehaviour {

	int TapCount;
	public float MaxDubbleTapTime;
	float NewTime;

	void Start () {
		TapCount = 0;
	}

	void Update () {
if (Input.touchCount == 1) {
			Touch touch = Input.GetTouch (0);
			
			if (touch.phase == TouchPhase.Ended) {
				TapCount += 1;
			}

			if (TapCount == 1) {
				
				NewTime = Time.time + MaxDubbleTapTime;
			}else if(TapCount == 2 && Time.time <= NewTime){
				
				//Whatever you want after a dubble tap	
				print ("Dubble tap");

					
				TapCount = 0;
			}

		}
		if (Time.time > NewTime) {
			TapCount = 0;
		}
	}
}

For whoever is reading this in 2020, since this is still the top ranking search result…

One of the first answers suggests that using tapCount is not supported in Android. This was way back in 2012 when Unity 3.x was in use and is no longer correct.

It’s perfectly supported now and has been since Unity 5.x (tested and using it on Android):

so doing something like this should be just fine:

void Update() {
    if (Input.touchCount <= 0)
    {
        return;
    }
    for(touch in Input.touches) {
        if (touch.tapCount == 2) {
             // your handler here
        }    
    }      
}

If you are looking for simple double tap, this might do the trick. I used this to simply set bool to true or false

if(Input.GetTouch(0).tapCount == 2){
     //Double tap
 }
        
if(Input.GetTouch(0).tapCount == 1){
     //Single tap
}

Not my code but this works on GameObject in Android and Windows:

float lastTimeClick;

public void OnPointerClick(PointerEventData eventData)
{
    float currentTimeClick = eventData.clickTime;
            
    if (Mathf.Abs(currentTimeClick - lastTimeClick) < 0.75f)
    {
            //double-click happened
    }

    lastTimeClick = currentTimeClick;
}

i’m using EasyTouch and it works great on android.
check there gestures list they can handle (note On_DoubleTap…)
http://www.blitz3dfr.com/Doc/ET3/class_easy_touch.html

just to make it clear, i’m not related to them… only suggest the as solution. :slight_smile:

As Vandell suggests, tracking your own touches is the go.

For quick code sample - Check the Joystick.js file in the mobile standard assets. In a nutshell:

  • Set a time window - say avg. human reaction time
  • Store your own script level scoped ‘tapCount’ variable
  • In update, on each tap, test if this tap occurred within the time window, if yes, tapCount++

From Joystick.js

    if ( tapTimeWindow > 0 ) {
        tapTimeWindow -= Time.deltaTime;
    }
    else {
        tapCount = 0;
    }
    if ( tapTimeWindow > 0 )
      tapCount++;
    else
    {
      tapCount = 1;
      tapTimeWindow = tapTimeDelta;
    }

HTH

There is a goal to compare times at touch in and touch out. If condition true and double touch not exist we set a double touch case. Then touch case is end we reset the double tap.

if (Input.touchCount > 0) {
touch_in = Time.time;
	if (touch_in - touch_out < 1f) && (double_tap == false){
		double_tap = true;
	}
	if (double_tap == true){
	//do whatever
	}
} else {
	touch_out = Time.time;
	double_tap = false;
}

I newbie in C# and Unity and will glad your code observation

BR

Since this is still a top search result and full of old, outdated approaches, I’m updating a current one

using UnityEngine.EventSystems;

//put this script on a gameobject that is a Raycast Target, like a UI Image
    public class CardTouch : MonoBehaviour, IPointerClickHandler
    {
    	public void OnPointerClick(PointerEventData eventData) {
    
    		if (eventData.clickCount == 1) {
    			FirstTap();
    		}
    		else if (eventData.clickCount == 2) {
    			DoubleTap();
    		}
    		else {
    			//ignore spamming clicks
    			return;
    		}
    	}
    
    	private void FirstTap() {
    		Debug.Log("Single tap on " + this.gameObject.name);
    		//do something
    	}
    
    	private void DoubleTap() {
    		Debug.Log("Double tap on " + this.gameObject.name);
    		//do something else
    	}
}

try to use LeanTouch asset. Its easy to use and have double tap option

@Seth-Bergman s way works just fine, no need for long lines of code, at least as of unity 2018.2 and sdk26,@Seth-Bergman s way works, just use
Input.GetTouch(0).tapCount or a for loop like he did. No long lines of code are necessary,at least with unity 2018.2

You could search harder…

http://docs.unity3d.com/Documentation/ScriptReference/Touch-tapCount.html

as in:

for(touch in Input.touches){
if(touch.tapCount == 2)
//DoSomething();
}