Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Omegamar · Jul 09, 2019 at 04:38 PM · script.mobileinput.touch

Is there a way for my touch input to only detect the most recent touch?

So both inputs work when they are done individually, but if both are pressed simultaneously then they cancel each other out. I would like if only the most recent touch would work so that they don't cancel out. Here is my code:

     private void Start () {
         rb = GetComponent<Rigidbody2D>();
         moveSpeed = 11f;
         verticleSpeed = -15f;
     }
     
 
     private void Update () {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             switch (touch.phase)
             {
                 case TouchPhase.Began:
                     if (touch.position.x < Screen.width / 2)
                         rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
                     if (touch.position.x > Screen.width / 2)
                         rb.velocity = new Vector2(moveSpeed, verticleSpeed);
                     break;
 
                 case TouchPhase.Ended:
                     rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
                     break;
             }
         }
     
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
1

Answer by Bunny83 · Jul 09, 2019 at 06:42 PM

This is a common error when dealing with touches. Each touch has a fingerId which actually identifies a finger from the moment it got pressed down (TouchPhase.Began) until the touch ended (TouchPhase.Ended or TouchPhase.Canceled).


If you only want to react to the latest touch you have to iterate through all touches and store the fingerID of the last "began" event. When you get a touch "Ended" you only set speed to 0 when the right last finger is released.

 int lastFingerId = -1;
 private void Update ()
 {
     foreach (Touch touch in Input.touches)
     {
         switch (touch.phase)
         {
             case TouchPhase.Began:
                 if (touch.position.x < Screen.width / 2)
                     rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
                 else
                     rb.velocity = new Vector2(moveSpeed, verticleSpeed);
                 lastFingerId = touch.fingerId;
                 break;
             case TouchPhase.Ended:
                 if (touch.fingerId == lastFingerId)
                     rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
                 break;
         }
     }
 }

This way releasing the second last finger will not have any effect.

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 BeanBugDev · Jul 09, 2019 at 07:58 PM

Can't you just.. Touch touch = Input.GetTouch(Input.touchCount - 1)

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 Bunny83 · Jul 10, 2019 at 01:34 PM 1
Share

No because the touches do not necessary have a particular order. In fact on my Nexus 7 the touches actually seem to be ordered by the finger id. Though you have finger 0 and 1 and release finger 0, the finger 1 will be at index 0 since it's the only touch still active. However if you place down another finger, it will get the finger id 0 again and therefore be at index 0 (at least on my device). I've created a simple Unity app long time ago which just visualizes all touches and displays all the information for each touch

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

173 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

please please please very urgent, can someone convert this script for android swipe 0 Answers

How to make Touch Input act like GetAxis? 0 Answers

convert script to mobile input 0 Answers

How to make a flawless rhythm-like mechanism in mobile 2D? 0 Answers

How do I detect touch on another object and check by tag/name? 1 Answer


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