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
3
Question by PresidentThompkins · Feb 28, 2014 at 05:25 AM · androidinputtouch

Differentiating between touches for multiple game objects.

For starters, I apologize if this has been asked. I've spent the last week scouring Unity answers, the script reference, and even referring to my own past code to see if I can break through this... It has be scratching my head still!

So, I have 3 objects on screen that need to be controlled. They are all in separate parts of the screen, and if a touch begins in their region they're supposed to do some relevant actions.

Problem I'm having is that despite them tracking well, if I begin or end another touch, in a different region, it freezes up my first object.

To put this in a less mundane language...

if object 1 is selected and moving... I try to use object 2... Object 1 freezes.

If I release object 2, object 1 stays frozen until I touch it again.

I'm thinking this is a problem with how I'm accessing my touches, however most multitouch answers seem to be able instantly destroying or creating an object with the touch, whereas I'm trying to track the touch, and maybe even save it as a variable. (Which Unity doesn't seem to like when I reference a variable in my If () Statements.)

So, lets toss up some pseudo code so I can better illustrate this problem...

obj1 and obj2 have the same script on them.

      foreach(Touch touch in touches)
         {
             if (touch.phase == TouchPhase.Began && isWithinTouchRegion && isNotSelected)
     {
     dostuff;
     }
     
        if (touch.phase == TouchPhase.ended)
     {
     fireashotandendtouch();
     }
     
 }


Okay so... This all looks find and dandy right?

I realize that I'm lacking a way to accurately track which touch I'm wanting to use, and I think that's where the root of my problem is. Referencing to http://answers.unity3d.com/questions/326253/strange-touch-behavior.html Helped to allevitae some stuff (For instance, these are cannons I'm controller, and obj1 no longer fires when obj2's touch ends), but I'm still getting just generally weird behavior.

I suppose I'm trying to get a better understanding of how Unity differentiates touches :/.

Comment
Add comment · Show 1
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 whydoidoit · Feb 28, 2014 at 05:50 AM 1
Share

You need to store and use the fingerId from the touch and use that to verify what the current state of that touch is. You will want to temporarily associate a fingerId with an object until that touch ends and then only adjust that object for touches that have a matching fingerId.

1 Reply

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

Answer by PresidentThompkins · Mar 01, 2014 at 04:45 AM

I woke up this morning with a solution in my head, and it worked! You're suggestion to track the finger ID for each touch state worked beautifully. I'm honestly half surprised that nobody in all the answers I read before suggested something like that.

For reference sake, to anybody wanting to see actual code...

  public int trackTouch;
     
     void Update()
     {
     foreach(Touch touch in Input.touches)
     {
     if (touch.position == *intherangeyouwantit* && touch.phase.Equals(TouchPhase.Began))
     {
     trackTouch = touch.fingerId;
     }
     
     if ((touch.phase.Equals(TouchPhase.Ended) || touch.phase.Equals(TouchPhase.MoveD)) && touch.fingerId == trackTouch)
     {
          DoStuff();
     }
     }
     }



Hope this helps someone later on!

Can a mod please mark this as solved?

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

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

21 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

Related Questions

Android - touch a 3d object and something happens to it 1 Answer

Touch Input madness 2 Answers

input touch android help 0 Answers

Touch Input, user can touch across the screen, not wanted 1 Answer

Touch Input in Android... 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