- Home /
How to make an object explode?
Hey guys,
I am making an aircraft game, and i want it so that when the aircraft hits the ground, it explodes.
I have downloaded the explosion framework: http://unity3d.com/support/resources/unity-extensions/explosion-framework, but i can only seem to get the explosion to work after a ceratin amount of time, does anyone know of a tutorial or a method in which i can use it to make the explosion occur on collision.
Or if the explosion framework can't do this, is there another way to make explosions????
Thanks
-Grady
Answer by FourSheds · Jul 07, 2011 at 10:21 AM
Hi
Assuming you already have the Detonator script attached, and 'Explode On Start' is disabled, then you can call Detonator's Explode() function within your OnCollisionEnter() method like this:
gameObject.GetComponent("Detonator").Explode();
ok, thanks, that works, but after that line, i put: Destory(gameObject); so my script looks like:
function OnCollisionEnter(collision : Collision){ if (collision.gameObject.tag == "terrain"){ gameObject.GetComponent("Detonator").Explode(); Destroy(gameObject); } }
but that just destroys the object and no explosion goes off....
I also find the below script on the internet:
// A grenade // - instantiates a explosion prefab when hitting a surface // - then destroys itself
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision){ // Rotate the object so that the y-axis faces along the normal of the surface var contact : ContactPoint = collision.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal); var pos : Vector3 = contact.point; Instantiate(explosionPrefab, pos, rot); // Destroy the projectile Destroy(gameObject); }
You just attach it to the game object and then you put the explosion prefab that you want in their. That works too, but when my aircraft hits the ground, it destorys and the explosion plays, but the explosion is a little bit of center, so the explosion plays in a different place to where the aircraft actually crashed....
If you can fix the above script then that would be good, but if you have any ideas for the first one, that would be great too!!!!
Thanks
-Grady
Not sure about the 2nd script without trying it, but in the first method, you could put a time delay: Destroy(gameObject, 5). Or you could put the Detonator script on an empty game object in your scene (that doesn't get destroyed) and call Explode() via that.
You're welcome! Hope this answered your question, hint, hint ;)
Answer by Ailos · Jan 12, 2012 at 08:07 PM
hello! I'd like to know , how can I do, that the explosion slowly dissipate? not, suddenly dissapear!
thanks a lot!
Your answer
Follow this Question
Related Questions
Detonation of an object on collision of character.... 1 Answer
Can an object to transfer to another as it increases your speed? 1 Answer
How can I do a collision check with a specific Object? 3 Answers
Object only explodes when it collides with certain object, not all rigidbodies. 2 Answers
how to check if one object is colliding with another 1 Answer