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 /
avatar image
0
Question by Paul36 · Nov 08, 2012 at 06:19 PM · mobiletouchscreenmulti-touch

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

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Nov 08, 2012 at 06:29 PM

 for (var touch in Input.touches) {
     if (touch.tapCount == 2) {
         // double-tap
     }
 }
Comment
Add comment · Show 7 · 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 Paul36 · Nov 08, 2012 at 11:07 PM 0
Share

@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!

avatar image Eric5h5 · Nov 08, 2012 at 11:57 PM 0
Share

You could check touchCount and loop through them using Input.GetTouch().

avatar image Paul36 · Nov 09, 2012 at 01:36 PM 0
Share

@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 ?

avatar image Eric5h5 · Nov 09, 2012 at 02:44 PM 0
Share

See the code examples in the docs for Input.GetTouch.

avatar image Paul36 · Nov 09, 2012 at 03:12 PM 0
Share

@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

Show more comments

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

12 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

Related Questions

Problem about detecting screen touch on mobile app. 0 Answers

Any ideas why my buttons work in the editor but not on mobile? 0 Answers

Can't solve hitbox issues in mobile game 1 Answer

2 Objects spawning instead of one at each update 2 Answers

Swipe and touch alternation 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