Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
9

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
Add comment · 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
5

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
Add comment · 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 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
3

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
Add comment · 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: Never$$anonymous$$d, 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
2

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
Add comment · 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
2

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
Add comment · 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
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

Android touches pass through UI elements 6 Answers

Touch once to activate and touch again to deactivate gameObject using Renderer 1 Answer

Two touches not working as expected 0 Answers

how can i make simple combo with ui button,, 1 Answer

Script to run on tap, exempt in area. 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