- Home /
Airplane - Collision With Terrain Problem
I wrote a script for my plane to be destroyed and explode when it collides with the terrain but it doesn't work. The plane has a rigidbody, and I tested out with the Is Trigger option off and on. Can anyone help me out? Thanks.
var explosion : ParticleEmitter;
function OnCollisionEnter (collide : Collision)
{
if(collide.gameObject.tag == "Terrain")
{
Destroy(gameObject.Find("F-16"));
Instantiate(explosion, transform.position, Quaternion.identity);
}
}
Answer by aldonaletto · Aug 23, 2011 at 11:37 PM
Is this script attached to the airplane? If that's true, use Destroy(gameObject); since this will "suicide" the script owner - the airplane, in this case. The way you did it should work too, but you should avoid GameObject.Find because it's too slow, and a common source of errors too.
Anyway, a possible reason for this to fail is the terrain tag: the terrain is untagged unless you explicitly tag it - and you must create the tag Terrain, since it's not included in the default tag list.
Check the terrain tag, and if it's ok place a debug line before the if - something like print("Hit something"); - to refine the bug search.
I tried the method and the message didn't pop-up when the plane collided into the terrain.
And what about the terrain? Was it tagged "Terrain"? And is this script attached to the F-16 object?
Yes, I tagged the terrain and attached the script to the F-16. I still get the same result.
It should work. I tested this same script with a block named F-16 and the changes I've suggested, and the block was destroyed as soon as it touched the terrain.
Are you using the Unity terrain? Click the terrain and verify at the Inspector if it has the Terrain Collider component, and be sure IsTrigger isn't checked.
Your answer
Follow this Question
Related Questions
Gameobject collision with Terrain C# 4 Answers
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers
changing color on collision -1 Answers
Physics.OverlapSphere() not working after using pool of objects 0 Answers
Generate collision mesh from 2d texture? 0 Answers