- Home /
Windows Multitouch Fail
Sorry in advance for the long post, but I promise it's an entertaining read :P
I'm encountering a real strange issue with multi-touch input on a Windows 10 tablet.
Firstly, a bit of background... The following method builds a Dictionary of Touches organised by the GameObjects that they are touching:
public Dictionary<GameObject, List<Touch>> TouchedObjectsWithTouches()
{
Dictionary<GameObject, List<Touch>> objs = new Dictionary<GameObject, List<Touch>>();
// MULTI-TOUCH INPUT
for (int i = 0; i < Input.touchCount; i++)
{
RaycastHit2D hit = Physics2D.Raycast (
Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position),
Camera.main.transform.forward
);
GameObject hitObj = hit.transform.gameObject;
if (hitObj != null)
{
if (!objs.ContainsKey(hitObj))
objs[hitObj] = new List<Touch>();
objs[hitObj].Add(Input.GetTouch(i));
}
}
return objs;
}
For example, with 3 touches on object "A", and 2 touches on object "B", the resulting Dictionary would look something like this:
A : { Touch0, Touch1, Touch2 }
B : { Touch3, Touch4 }
The order of touches doesn't matter.
Now, this all works absolutely perfectly on Android. Unfortunately, the people I'm doing this project for want it to run on both Android and touch-capable Windows devices.
When I make the build and test it on the Windows device, it doesn't identify the touched objects correctly. Using the same example as above, with 3 touches on "A" and 2 touches on "B", the resulting Dictionary looks like this:
A : { Touch0, Touch1, Touch2, Touch3, Touch4 }
If I then slide all of the touches on "A" onto "B", the dictionary DOESN'T CHANGE. It's still reporting that all 5 touches are on "A", when they clearly aren't. What gets stranger is that when there's only 1 touch, the thing works fine - put a finger on "A" and dictionary has A : { Touch0 }
, slide that touch to "B" and dictionary changes to B : { Touch0 }
...
But wait, there's more!
Lets say all the touches start on "A" ( A : { Touch0, Touch1, Touch2 }
), I slide all the touches onto "B" and the dictionary doesn't change. But I keep sliding those touches until they're waaaay away from "A", and finally the dictionary updates to B : { Touch0, Touch1, Touch2 }
...
BUT WAIT... THERE'S MORE!
Again, say the touches start on "A" ( A : { Touch0, Touch1, Touch2 }
), and I add another touch waaaay over where I got the touches to work last time. What do you think happens then? B : { Touch0, Touch1, Touch2, Touch3 }
of course! Never mind the fact that 3 of the touches ARE STILL ON "A". LOGIC!
Okay, but seriously... what the heck is this? I'm pretty sure there isn't anything wrong with my code - at least it doesn't look like it. I mean, it works absolutely perfectly on Android...
Any ideas? At all?
Thanks in advance.
Did you test that on different tablets? Some cheap tablets only support two distinct touches. Do you have any other mul$$anonymous$$ch demos / apps to test if the touch works correctly on that device?
Unfortunately I only have that one Windows tablet to test it on. I know for a fact, however, that this tablet supports at least 10 simultaneous touches; earlier in development I wrote a short code snippet to display the x,y co-ordinates of each of the active touches, they all reported their individual, distinct locations correctly (and they still do). Because of this, I'm wondering if it's something related to the raycast... Either way, it's definitely not a limitation of the tablet.
Also should mention there is a separate part of this same project which requires tapping on up to 4 or 5 objects at once (tested thoroughly and successfully) - it's using a different block of code for input, though it bears some similarities to the one mentioned in the original post - and it works fine.
Edit: I just tested this other mul$$anonymous$$ch part of the project again, and now it isn't working, BUT an older build with the exact same input code does work.... Now I'm really confused x_x
Okay... I have some further information.
Firstly, I wondered if it was caused by an asset that I had imported, but there was only one of those (LeanTween), and the problem still persisted after removing it.
After further investigation, it seems that the object that all the touches get associated with is the object that's at the average position / centrepoint of all the touches - hence why, in the above examples, when I add the touch waaaay away from object "A", it then starts reporting object "B" - "B" is now at the centrepoint of all the touches.
That also explains why it continued to report "A" when I moved all touches off "A" ... I moved them off in a circular motion, which of course left "A" as the centrepoint. If I move all touches off "A" so that they're all still near each other, then "A" stops being the reported object as it's no longer at the centrepoint.
Well, at least I know how it's selecting which object to associate the touches with... Still no idea why though.
Answer by eldorno · Oct 12, 2016 at 06:03 AM
Alright. Problem solved.
After all this, it turns out that somewhere else in the code I was using Input.touchSupported
to check whether to use touch or mouse input... This always returns false
on Windows devices, regardless of whether or not they actually do support touch.
Thanks Unity.
Anyway, getting rid of that solved the issue and now the multitouch works as expected on both Android and Windows tablets! Wew!