Problem with touch detection
Hi.
I'm using the function OnMouseDown() to detect when the object has been touched and destroy it, My code is:
void OnMouseDown(){
GetComponent<AudioSource> ().Play ();
NotificationCenter.DefaultCenter().PostNotification(this, "addScore", myPoints);
Invoke ("destroyBall", 0.1f);
}
void destroyBall(){
Destroy (this.gameObject);
}
The code works in the computer, but when I test it in my phone, sometimes works, and sometimes doesn't work. I test in anothers devices and it's the same.
Any idea?
please be more precise on what does not work.
if the clip you're playing is .1f seconds long, this code is fine, but you should replace the invoke delay by
GetComponent<AudioSource> ().clip.Lenght
Test in which cases it does not work. For instance, maybe it works with a single touch, but multi-touch gives errors..?
I tested the game with only single touches, but still happens... $$anonymous$$aybe the problem is the detection of the collision, but I don't know how improve it.
Answer by hyruleoflink · Sep 16, 2015 at 09:51 AM
I finally solved the problem. The cause was the Unity's physics engine. I used the gravity to move the object, but If I use the variable "velocity" fron Rigidbody2D, the game works well.
Your answer
![](https://koobas.hobune.stream/wayback/20220612033657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tetris grid 1 Answer
How can I detect touch on a Mesh Collider on an Android device ? 3 Answers
Create an array of touches in Android? 0 Answers