- Home /
Spawning an explosion using an IF statment
I'm very sorry if this has been asked before, but I can't seem to figure this out.
Basically I want an explosion to spawn at the location of where an object is destroyed. I know this is a rather simple thing to do, but the script I've got for collisions is applied to the player.
I was wondering how I would write an IF statment in the script that is applied to the player to make an explosion spawn at the location of the destroyed objects.
Thanks for your time :)
So, if a rock hits the player, you want an explosion right at that point? Or you want the rocks to explode where they hit, no matter what they are hitting? In general, put the explosion spawn (and die) on the object being destroyed, not on the player.
Answer by Sebas · May 05, 2011 at 10:37 PM
From the documents: "In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider."
Taking this further, you can access collision information such as the gameObject or collider that you hit. Once you have the gameObject, you can get the position/transform, spawn an explosion, do other stuff...
function OnTriggerEnter(collisionInfo : Collision)
{
if (collisionInfo.gameObject.tag == "ExplodingGameObject"){
//explode
//use collisionInfo.transform.position to grab the location you need)
}
}