How to detect Double Tap in Android
Hello All
I need some code to detect double tap?
Answer by Bouzy · Feb 15, 2017 at 08:40 PM
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;
}
}
}
Answer by pedrampk · Jul 13, 2018 at 06:26 PM
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;
}
The code looks good, but for some reason it registers a double tap on the first touch?
Answer by vedraan · Jan 26, 2020 at 10:20 AM
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): https://docs.unity3d.com/520/Documentation/ScriptReference/Touch-tapCount.html
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
}
}
}
I tried something similar. The tapCount property isn't going above 1 for me. Also, I'm using my iPad hooked up as a second monitor through Duet and not Unity Remote, if that matters, but it isn't stopping other touch related stuff from working as far as I know.
EDIT: Never$$anonymous$$d, I just played with Unity Remote on my iPhone and tapCount worked on that. It must have just been Duet.
It's 2021 - and sadly IPointerClickHandler still doesn't work on android - I don'T want to react to input in a god damn update loop like its the 1980s.
Answer by Jarkko129 · Apr 20, 2021 at 03:11 PM
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
}
Answer by dageddy · Sep 29, 2021 at 05:23 AM
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;
}