Since reading the question and answer below, I tried checking to see if the array has something in it, and if it did, dest...

posted to: Exploding Grenades
Since reading the question and answer below, I tried checking to see if the array has something in it, and if it did, destroy the grenade. That stopped the blocks from being exploded repeatedly! XD I don't know any better way to do that yet.
  • Jonathan Gonzalez(jgonzalez) replied
    Yes the grenade should be destroyed but I would instead put a timer within the Start function of say 5 seconds or so then have it destroyed. This way the grenade would destroy itself anyways even if not rigidbodies are within the explosion radius.
  • jazze replied
    Oh, okay! I'll modify the code for that, thanks! :D
  • jazze replied
    Hmmmm, how do I get it to actually work? Do I need to make an array in Start, instead of the Explosions function? It seems not to work anywhere else but where I first put it. :(
  • Jonathan Gonzalez(jgonzalez) replied
    Sorry I should've been more clear. You can add the destroy line in a few places. Either in the Start function as such: void Start () { Destroy(gameObject, 5f); } or after the foreach loop in the explosions function like such: void GrenadeExplosion () { Vector3 explosionPos = transform.position; Collider[] col = Physics.OverlapSphere (explosionPos, radius); foreach (Collider obj in col) { if (obj && obj.attachedRigidbody) obj.attachedRigidbody.AddExplosionForce (power, explosionPos, radius, liftPower); } Destroy (gameObject); } Note that in this case I just have it instantly destroyed once this function is called. So if I called that function from "OnCollisionEnter" it would then explode and destroy the grenade at the same time to create a more realistic effect. Hope that helps.
  • jazze replied
    Oh, okay! It seems like the second method works best, but I made a slight delay of 0.4f because it would get destroyed before the explosion happened. ;_; Thanks!