I have a car with a rigid body running into the model, Lerpz_new, also with a rigid body. I want the car to hit Lerpz and for Lerpz to fly backwards for 3 seconds before disappearing using the following code on the car. The Force values are arbitrarily chosen:
function OnCollisionEnter(collidedObject : Collision) {
if (collidedObject.gameObject.tag == "Lerpz_new") {
collidedObject.rigidbody.AddForce(1000, -1000, 1000);
yield WaitForSeconds(3);
Destroy(collidedObject.gameObject);
}
}
All that happens is the car rams into Lerpz and stays there. Any clues?
Try testing whether the tags and collision stuff is set up properly. Add Debug.Log("hit"); as the first line, and Debug.Log("lerpz"); just after the if. If you don’t see HIT, it’s a collision problem. If you only see HIT, it’s a tag-checking problem. Only if you see both is it a problem with your “push” code.
Also, as per fafase’s comment, you should only have WaitForSeconds in coroutines. C# would throw an error about it in collisionEnter. Javascript probably just aborts. The timed Destroy is a cheap, legal alternative.
[in response to comments]
You can use more debugs to narrow down a long path by checking each part: