• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by anilgautam · Aug 11, 2017 at 07:32 AM · touchdouble-tap

How to detect Double Tap in Android

Hello All

I need some code to detect double tap?

Comment
pmso
hantersuxx

People who like this

2 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

12 Replies

  • Sort: 
avatar image

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;
         }
     }
 }
 




Comment
awaywing
hantersuxx
lauraaa
Olegjan17
PerfectSkies
AzariDeveloper
sepehrgoodman2000
nandanramesh13
ArianSadafi
asege98
zanatos90

People who like this

11 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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;
     }
Comment
SpaharGR
nizameddinkale
Mym2o
Andre_Santos
Brother_77

People who like this

5 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brother_77 · Feb 01, 2022 at 07:07 PM 0
Share

The code looks good, but for some reason it registers a double tap on the first touch?

avatar image

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
         }    
     }      
 }




Comment
bektasali96
coder9401
Andre_Santos
mrtorrent

People who like this

4 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image testmaster217 · Jan 30, 2020 at 11:57 PM 0
Share

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: Nevermind, I just played with Unity Remote on my iPhone and tapCount worked on that. It must have just been Duet.

avatar image IARI · Feb 15, 2021 at 08:06 PM 0
Share

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.

avatar image

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
 }
Comment
AonaS
CypherXd

People who like this

2 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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;
 }
Comment
AonaS
Zanoshky

People who like this

2 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • 3
  • ›

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make touch input work on my IPhone? 1 Answer

Touch Controls without virtual joystick 0 Answers

Show / Hide GUI Button - Enable / Disable Script (Android) 0 Answers

Dual touch controls very inacurate, even with low sensitivity 0 Answers

Free 3d Camera rotation around a 3d object with touch inputs. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges