Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 wolodo · Nov 22, 2012 at 10:21 AM · clicktap

Android Tap detection

Hi,

I have a little bit odd issue with detecting touch inputs. I am trying to implement something like click with mouse, that is:

-player touches an object

-he does not move his finger

-when he lifts the finger, something happens (in my case level loads)

My code to achieve this is here:

 var phase1 :boolean = false;
 function Update () {
     if(Application.platform == RuntimePlatform.Android) {
         if(Input.touchCount > 0) {
                 var hit : RaycastHit;
                 var vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
                 //var fingerPos = Vector2(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y);
                 var ray : Ray = Camera.main.ScreenPointToRay (vektor);
                 if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
                     if(hit.collider.gameObject == gameObject) {     
                         if (Input.GetTouch(0).phase == TouchPhase.Began){ 
                             phase1 = true;
                         }   
                         if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Canceled){ 
                             phase1 = false;
                         }
                         if (Input.GetTouch(0).phase == TouchPhase.Ended && phase1 == true && getPrevLevelCompleted(gameObject.name)){ 
                             phase1 = false;
                             loadingScreen.guiTexture.enabled = true;
                             PlayerPrefs.SetInt("lastPlayedLevel", getCurrentLevelNumber(gameObject.name));
                             Application.LoadLevel(gameObject.name);
                         }
                     }                           
                 }
         }
     }
 }



The problem is, that this solution works on some devices, however on some devices not. For example on some cell phones player needs to tap on an item fast repeatedly several times to trigger an event. What do I do wrong? Is there some standard solution/function to implement 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wolodo · Nov 22, 2012 at 10:36 PM

So I managed to solve it by myself. I modified my source code like this:

 private var phase1 :boolean = false;
 private var touchCoordinates :Vector2;
 
 function Update () {
     if(Application.platform == RuntimePlatform.Android) {
         if(Input.touchCount > 0) {
                 var hit : RaycastHit;
                 var vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
                 //var fingerPos = Vector2(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y);
                 var ray : Ray = Camera.main.ScreenPointToRay (vektor);
                 if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
                     if(hit.collider.gameObject == gameObject) {     
                         if (Input.GetTouch(0).phase == TouchPhase.Began){ 
                             phase1 = true;
                             touchCoordinates = Input.GetTouch(0).position;
                         }   
                         if (Input.GetTouch(0).phase == TouchPhase.Moved){ 
                             if(Vector2.Distance(touchCoordinates, Input.GetTouch(0).position) > 10) {
                                 phase1 = false;
                             }
                             touchCoordinates = Input.GetTouch(0).position;
                         }
                         if (Input.GetTouch(0).phase == TouchPhase.Canceled){ 
                             phase1 = false;
                         }
                         if (Input.GetTouch(0).phase == TouchPhase.Ended && phase1 == true && getPrevLevelCompleted(gameObject.name)){ 
                             phase1 = false;
                             loadingScreen.guiTexture.enabled = true;
                             PlayerPrefs.SetInt("lastPlayedLevel", getCurrentLevelNumber(gameObject.name));
                             Application.LoadLevel(gameObject.name);
                         }
                     }                           
                 }
         }
     }
 }

Maybe it will be useful for someone.

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
0

Answer by CodeMasterMike · Nov 22, 2012 at 12:04 PM

Look at the documentation for GetTouch and you can see some "standard" sollution to how to use GetTouch.

A tip is that you loop through your touchCount and not just check the first one. Like the documentation says:

     for (var i = 0; i < Input.touchCount; ++i) 
     {        
      if (Input.GetTouch(i).phase == TouchPhase.Began)     
      {            
       clone = Instantiate (projectile, transform.position,  transform.rotation);        
      }
    }

Good luck!

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 wolodo · Nov 22, 2012 at 12:49 PM 0
Share

Thanks, but this doesn't seem to solve my problem. If I trigger an event at TouchPhase.Began, the user does not have a chance to cancel his tap. I want it similar like when you click on some html link or button with mouse. If you hold the button, nothing happens. If you release the button, the link is clicked however, if you push mouse button, wait some time or move away from the link, also nothing happens. (I now, there is missing any timer in my code, I will add it later) Shouldn't I add some latency to this action? For example tolerate some small movement of the finger?

avatar image CodeMasterMike · Nov 22, 2012 at 12:55 PM 0
Share

Well in the documentation, you will find there is a phase called Ended, which works the same way as a release of a mouse button.

http://docs.unity3d.com/Documentation/ScriptReference/TouchPhase.Ended.html

So do only your GetTouch checks if the phase is equal to Ended. Then you don't need any extra checks or time delays.

 for (var i = 0; i < Input.touchCount; ++i) 
 {        
     if (Input.GetTouch(i).phase == TouchPhase.Ended)     
      {            
         // Do your whatnots here...
      }
 }
avatar image wolodo · Nov 22, 2012 at 12:59 PM 0
Share

Yes, I know. I use it in my code. Still not sufficient. I need to have Begin phase on my object, not move and also Ended phase on the same spot.

avatar image CodeMasterMike · Nov 22, 2012 at 01:18 PM 0
Share

I would do it like so, that when the phase is ended, then I would take the Touch.position and do a raycast/my own collision check with the position to see if the player has touched a object or not. The begin phase come automaticly so you wouldn't really need that one.

But if I had to use Begin and move as well, I would do something like this:

 bool $$anonymous$$oved = false;
 bool Began = false;
 for (var i = 0; i < Input.touchCount; ++i) 
 {        
     if (Input.GetTouch(i).phase == TouchPhase.Begin)     
     {
         Began = true;
     }
     if (Input.GetTouch(i).phase == TouchPhase.$$anonymous$$oved)     
     {
         $$anonymous$$oved = true;
     }
     
     
     if (Input.GetTouch(i).phase == TouchPhase.Ended)     
      {            
         if(Began == true && $$anonymous$$oved == false)
         {
             // Do your whatnots here...
         }
         else
         {
             return;
         }
      }
 }
avatar image wolodo · Nov 22, 2012 at 01:27 PM 0
Share

Actually this code is almost same as $$anonymous$$e. Please take a look at it. $$anonymous$$y problem is, that it wasn't fully working. Especially with devices with displays with higher ppi. So I was wondering if most of the taps on these devices make also small movement. That could be the answer to my problem but I am not sure...

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

11 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

Related Questions

Tapping iAd via AdMob is triggering RANDOM clicks on sprites in my scene. 1 Answer

Trying to detect mouse click or tap on a gameobject, why would not it work? 2 Answers

Mobile Tap Issue 1 Answer

Tap to click on Mac touchpad 0 Answers

Player to look at 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