I apologize if this isn’t the best place to be posting this… I’m developing a game for Android and iOS devices that requires the player to tap individual objects on the screen as they appear. Missing the object results in losing the game.
Now, I have one feature I’d like to include where multiple objects appear at once, and the player must tap them both simultaneously. I’ve written some basic 2D Raycasting code to detect this, and it seems to work perfectly… sometimes…
It seems some times the multi-tap is registered and all goes well, other times it seems a third tap is registered halfway between the two touch points, and since this point does not collide with the two objects, this results in the player losing.
To be more specific, here is a generic version of the code I am using to detect multiple taps.
if (Input.touchCount == 2) {
if (Input.GetTouch(0).phase != TouchPhase.Stationary ||
Input.GetTouch(1).phase != TouchPhase.Stationary)
return;
Vector2 v1 = Camera.main.ScreenToWorldPoint(new Vector3(
Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
Vector2 v2 = Camera.main.ScreenToWorldPoint(new Vector3(
Input.GetTouch(1).position.x, Input.GetTouch(1).position.y));
RaycastHit2D h1 = Physics2D.Raycast(v1, Vector2.zero);
RaycastHit2D h2 = Physics2D.Raycast(v2, Vector2.zero);
if (h1.transform != null && h2.transform != null) {
// TODO: do stuff
}
}
Single taps are handled in the individual object’s OnMouseDown functions, while multiple taps are handled in a single Update method for all objects.
Looking back, I think I can fix the issue of the player losing when this third tap is registered by ensuring there is only one touch point in the OnMouseDown function, or moving the code to this Update function and doing it all with Raycasting. But I guess my real question is, how reliable is this? Is there a way to ensure the game is only available for download from Android and iOS devices that support two distinct touch points? I remember reading somewhere that the hardware design of certain mobile touchscreens don’t account for multiple touches if they are on the same row or column, and I obviously need this to work 100% of the time.
Could you point me to some resources on this? Or am I better off leaving out this feature? Thanks!
EDIT: The game has been testing on a Samsung Galaxy S3 API level 19 and an iPad Mini iOS 9.2. They have identical results.