- Home /
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 :/.
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.
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220613134950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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