In Unity 5.3, how do I detect a touch on a sprite in a 2D game?
I am making a game for pre-literate kids. I need to move to the next scene automatically when they have touched all the specific sprites in a scene. Because kids will poke at everything, I need to identify the touch on the sprite and, if it has not been touched before, increment a counter. Then, when all the correct sprites in the scene have been touched, I'll move to the next scene. But I have been unable to detect the touch on the sprite. Are there any example scripts?
Answer by ElRobin · May 26, 2016 at 01:00 PM
I used the button approach for another scene, but it means there is still a boundary around the sprite, so that would not be as desirable for this scene.
The second choice is my preferred one, but I can't seem to write a script that is correct that detects when the colliders are touched. An example script would be much appreciated.
The idea of disabling the script once the sprite is touched is great. Thanks.
What I've been doing for all my buttons:
void On$$anonymous$$ouseOver()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
//Do stuff
}
}
Technically, I think you're supposed to use GetTouch(), but it works...
That was nice! Worked. This wouldn't work on mobile, right!?
Answer by Thajocoth · May 25, 2016 at 05:09 PM
There are several ways to do this.
You could make them buttons that call a function when touched. If the function requires an ID, and each sprite passes a unique ID to the function, you could keep track of which IDs have already been touched.
You could add colliders to the sprites and a script that detect when the colliders are touched. The script could send a signal to a central script when it's touched and then disable itself, so further touch of that sprite would not be detected.