- Home /
 
Destroy On Collision
I got a rocket that follow you until it collide with something and explode. I wrote a simple code for the collision. The problem is that the rocket don't explode when it should: it fly through walls , touch me but continue to fly around, it explode only when I shoot on it.
The code I wrote shoulde make it explode when it collide with anything that have a collider, trigger or not:
 var explodPrefab : Transform;
 var spawnPoint : Transform;
 
 function OnCollisionEnter (hit : Collision)
 {
    var exp = Instantiate(explodPrefab, spawnPoint.transform.position, Quaternion.identity);
    Destroy(gameObject);
 }
 
 function OnTriggerEnter (hit : Collider)
 {
    var exp = Instantiate(explodPrefab, spawnPoint.transform.position, Quaternion.identity);
    Destroy(gameObject);
 }
 
 
               Note: all the game objects have a collider on them.
Why this is happening?
collision will detect only for those body which have collider, e.g if "A" is colliding with "B" then there must be a collider with both A & Bm have don this? Note: A & B are game objects.
Answer by uvavoo · Dec 05, 2012 at 08:14 PM
Try the script from the first person shooter tutorial. Script also assumes you have a particle emitter attached for the rocket trail. I use this and it works perfectly.
 // The reference to the explosion prefab
 var explosion : GameObject;
 var timeOut = 3.0;
 
 // Kill the rocket after a while automatically
 function Start () {
     Invoke("Kill", timeOut);
 }
 
 
 function OnCollisionEnter (collision : Collision) {
     // Instantiate explosion at the impact point and rotate the explosion
     // so that the y-axis faces along the surface normal
     var contact : ContactPoint = collision.contacts[0];
     var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
     Instantiate (explosion, contact.point, rotation);
 
     // And kill our selves
     Kill ();    
 }
 
 function Kill () {
     // Stop emitting particles in any children
     var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
     if (emitter)
         emitter.emit = false;
         
 
     // Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
     transform.DetachChildren();
         
     // Destroy the projectile
     Destroy(gameObject);
     
 }
 
 
 //@script RequireComponent (Rigidbody)
 
              Answer by kaolas · Nov 30, 2012 at 06:29 PM
Based on your description, I don't think your collision detection is working correctly. Do both your rocket and the object it is colliding with have colliders on them? Both will need a collider for a collision to be detected.
Yeah, the rocket need to destroy itself when it collide with anything that have a collider on it, even if the collider is a trigger. The rocket have a collider on it, it is not a trigger and the objects it need to collide with have a collider as well. The problem can be seen mostly when it go through the terrain.
Does the explosion prefab/effect get created successfully? If not, your hit code probably isn't being triggered. Your code in the question also says that the explodPrefab is a Transform. You can't instantiate a transform. It needs to be a GameObject. You may be getting an error when trying to instantiate the Transform and then it doesn't execute the Destroy()
Answer by neogrant2 · Dec 01, 2012 at 05:19 PM
Replace it with
 Destroy(hit.gameObject);
 
              But this will destroy the object the rocket hit and not the rocket...
I am using this code on the rocket, and the rocket should explode whenever it hit something with a collider, trigger or not.
Answer by uvavoo · Dec 05, 2012 at 08:14 PM
I read somewhere that for fast moving objects the collider will pass through objects without triggering a collision (ie gets past object between 'updates'). Try making the collider bigger or slowing down the rocket.
Your answer
 
             Follow this Question
Related Questions
How to add to a 3D object's length through code? 1 Answer
Overlap point of 2 kinematic colliders 1 Answer
How to add the right amount of points when my player destroys a gameobject? 0 Answers
Receiving information about getting hit by a Raycast 2 Answers
How do you execute Trigger-collider collision only in one gameobject? 1 Answer