- Home /
Missile Collision and Explosions
Okay so I want to make it so that when my missiles collide with something they explode(terrain, object, ect), how would I set this up? Note I have downloaded the Detonator extension.
Currently I've got targets that go down just from getting hit, how would I arrage for the explosion made (most likely would use either the simple or tiny explosion if I use a premade one) to be what knocks the targets down?
And if you would so kindly provide some example code for the above questions? I'm just barely starting out using Unity so if you just tell me where to put it I might not quite get it. Thank you ahead of time.
Here's something i copied from a longer script i wrote for a game. Basically the idea is you put this on your missile prefab and then when you instantiate it from a button or whatever it will fly forward on its Z axis and at whatever speed you put in the code (right now it's set to 100) Then when it hits something it will explode and destroy itself. It can also do something depending on what the Tag is of the object it hit (like add different scores for different objects)... I have not tested this exact script but it will point you in the right direction ;)
Answer by KrisCadle · Jul 26, 2011 at 10:25 PM
//attach your explosion prefab in the editor
var explosion : GameObject;
static var score : float;
//gives the missile its speed
function FixedUpdate () {
rigidbody.AddForce (transform.TransformDirection (Vector3.forward) * 100.0);
}
function OnCollisionEnter(collision : Collision) {
var contact : ContactPoint = collision.contacts[0];
var thisExplosion : GameObject = Instantiate (explosion, contact.point + (contact.normal * 5.0) , Quaternion.identity);
//here you can set a tag so when your missile hits different stuff it can add different scores or have different explosions
if (collision.gameObject.tag == "enemy")
{
score = score + 100;
Destroy (collision.gameObject);
Instantiate (explosion, contact.point + (contact.normal * 5.0) , Quaternion.identity);
}
}
Destroy (thisExplosion, 2.0);
Destroy (gameObject);
}
Your answer