- Home /
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...
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...
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);
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;
}
}
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.
Or I guess loop through the touches and find the one with the same id? Inefficient but simple
Your answer
Follow this Question
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