Why the collisions are detected randomly?
Hi everyone,
I am trying to detect the collision between two object. Basically there are 5 spheres that will collide whit a slightly moving object. First the first sphere, than I will put the gravity to the second one and so on. Everytime I destroy the collided sphere. In the last sphere I reset the scene. I print "collision detected (name_of_the_sphere)" everytime that the collision happens. The problem is that sometimes is really random. In particular the collision detected sometimes appear twice for some sphere, sometimes only once, then 2 times..how is it possible?
ps: the object on which the spheres impact is kinematic
void OnCollisionEnter(Collision col)
{
print("Collisione center");
GameObject altodx = GameObject.Find("altodx");
Rigidbody rb = altodx.GetComponent<Rigidbody>();
rb.useGravity = true;
//play the audio
cube.GetComponent<AudioSource>().Play();
GameObject center = GameObject.Find("center");
Destroy(center);
Well, you're never using the actual game object that collides with the rigid body.
You are never using the "col" variable, ins$$anonymous$$d you are getting the "altodx" GameObject which I assume can return any of the spheres named that in any order the engine decides to get them in.
You should be using
GameObject altodx = col.gameObject;
I tried but doesn't work..it still count random numbers of collision ins$$anonymous$$d of just one
Answer by Abhiroop-Tandon · Mar 23, 2016 at 12:20 PM
you need to check from what you are colliding so usually you either check by a tag or name, something like
void OnCollisionEnter(Collision col)
{
if(col.gameObject.CompareTag("Sphere")){
print("Collisione center");
}
}
So now it will know that yeah i collided with the Sphere and now i have to print "Collision centre".
Thank you. I tried something similar. Basically as you said I check when a sphere impact on a precise object, my kinematic object. But then I still have the same problem, sometimes it print me twice the "collision detected" and sometimes just once. And in this case I also have a delay on the collision of the first sphere (before being destroyed, that is, the sphere impact, then roll for a second on the collider and then it is destroyed)..
I also tried to put a bool variable as false, then as true inside the collision detection function, and a control that if the bool is true then print something. But again, sometimes it print twice and sometime only once. I really can not understand where the problem is!
Then under the rigidbody component of your game object change the Collision Detection property to continuous
O$$anonymous$$, I put the control you suggested and now it seems to work! YEAH! Thank you very very much! :)
Answer by pitbull3636 · Mar 25, 2016 at 02:39 PM
Not really solved...Is still random from time to time.. I don't know what else to try. So I am trying to disable the script after the first execution and re enable it later during the execution of the scripts of the other spheres.
UPDATE: after the collision I disable the detection of the collision like this: GetComponent().detectCollisions = false;
and then I reactivate the collision detection on another sphere. BUT STILL sometime it count 2 collision instead of one! is impossible!! Theoretically after the first one the detection is disabled! how is it possible that it print two times "collision detected"????!!
Your answer
Follow this Question
Related Questions
Make an object fly when it hits another 0 Answers
Check for collision ONCE 0 Answers
Collider seems bigger than it is 0 Answers
OnCollisionEnter Trigger 1 Answer