- Home /
Differentiating between fingers in multitouch
Sorry if the question title was vague/misleading/confusing, but I didn't know how else to write it. Say I have two fingers touching the screen, if one is pressing and holding, and the other is swiping, how can I determine which finger is doing which? As in, which one is Input.GetTouch(0), and which is Input.GetTouch(1)? Is there a way to figure it out so I can do both actions at once? That way if one finger is pressing and holding (say to continuously fire a weapon), and then another one goes to swipe (to rotate the camera), I'd be able to do both without confusing the script. And then if the one that is firing the weapon slides a bit, it would keep firing and wouldn't affect the rotation of the camera. All ideas welcome. Thanks.
Answer by Owen-Reynolds · Feb 22, 2014 at 10:00 PM
A cheap method is to start out by remembering "touch[0] is doing this" and "touch[1] is doing that." I've noticed the touches are stable on a iPad. In other words, if you touch with thumb then pinky, pinky will always be touch[1]
.
The tricky part is, if they let go of the first finger, the second finger drops down to be touch[0]. So, if you have two touches and touch[0] ends (which you can check,) copy your saved second finger status into first finger.
If some smart guy touches three times and releases #0, bad things might happen. May have to add "if touches more than 2, ignore all until touches drops down to 0."
Another way, more heavy-duty, is to use the touchID. It's assigned on touchBegin and will never change over the touch. But, an iPad at least, will reuse the lowest free one (I think.) Exs:
tap and hold 1st finger: touch[0] has ID 0
tap and hold 2nd finger: touch[1] has ID 1
Release 1st finger: 2nd finger is now touch[0], but still has ID 1
Release all fingers, touch again: touch[0] has ID 0 (reuses IDs)
You can "track" any touch, even though it may drop down from [2] to [1] to [0] (I've never seen one go up) by the ID.
I assume you mean fingerID... do you know how to use it though? I can't figure it out from the reference. For example, how would I find out position of fingerId(1)? Sorry for the late reply, and thanks for your help :)
And does it all have the same phase properties i.e. can I still use fingerId.phase == phase.moved or whatever?
The ID isn't an index. You have to do all the work yourself. When you see the touch began, save the ID. Each frame after that, the touches
loop can compare IDs to your saved ID1 or ID2. Or backwards, search the touches array for your saved ID.
But if the # of touches hasn't changed, and nothing has begun, no need.
Thanks Owen, took a bit of going but I got it sorted. Thanks for your help.
Awesome! I hadn't realised there was such a thing as Touch.fingerId. This saved me so much messing around. You RoCk! Thanks!
Your answer
Follow this Question
Related Questions
How to press 2 buttons at the same time 1 Answer
Deploying to Android or iPhone device - track public variables in inspector? 1 Answer
Specular Materials on iPhone or Android? 1 Answer
Simultaneous Android and iOS development 1 Answer
Is this the correct walkthrough to get a game from pc to android/iphone? 1 Answer