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
3
Question by Curial-Games-CEO · Aug 20, 2016 at 07:48 PM · c#mobiletouch

Input.GetTouch(index) is NOT the fingerID

Hello guys. I have the next issue:

On my game, I check if a touch is inside a zone and if it does, I save its fingerID in a variable, let's say finger, so that I can get its position on every frame.

The problem is that when I use Input.GetTouch(finger) I realized that the touch index is not always the finger id.

How do I know that? If I touch (finger index 0) and again (finger index 1) and then release the first one (index 0), the finger index 1 now is the 0, but it keeps its fingerID number (1) so it is the touch index 0 and finger id 1.

The question is, is there a way to make reference to the touch by its finger ID instead of its index?

Sorry if my English is not good...

Comment
Add comment · Show 3
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 Curial-Games-CEO · Aug 20, 2016 at 03:11 PM 0
Share

Well, obviusly I can make a loop through the Input.Touches array and ask if fingerID is the one I have saved, but I'm looking for a more efficient way...

avatar image Arkaid · Aug 22, 2016 at 08:42 AM 0
Share

No, there isn't, but you can find it with one line of code

 Touch touch = System.Array.Find(Input.touches, touch => touch.fingerId == finger);
avatar image Forlinim91 Arkaid · Feb 22, 2017 at 02:06 PM 0
Share

While this work, it's a bad idea: Input.touches allocate a new array every time (which the GC must destroy), so you should avoid it.

Better a multi-line (but more memory friendly) iteration:

for (int i = 0, n = Input.touchCount; i < n; i++) {
   Touch touch = Input.GetTouch (i);
   if (touch.fingerId == finger){
      //Do stuff
      break;
   }
}

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by _Yash_ · Aug 22, 2016 at 09:40 AM

No, there is no direct way of doing this.
What you can do is create a wrapper class of Input and have a dictionary of type { int fingerid, Touch t } which is updated by wrapper class for each update and use dictionary[touchid] whenever you need to access the touch.

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 shadowsora · Aug 13, 2017 at 05:54 PM 2
Share

Or I guess loop through the touches and find the one with the same id? Inefficient but simple

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

8 People are following this question.

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

Related Questions

IOS GUI slider 1 Answer

Touch not working on guiTexture. 1 Answer

Mobile game touch problem (C#) 2 Answers

How to make GUI pop up when you touch other gui 1 Answer

Multiple Cars not working 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