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

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #4789
    Jasmine Wongus
    Participant

      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.

      #23080
      Jonathan Gonzalez
      Participant

        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.

        #23081
        Jasmine Wongus
        Participant

          Oh, okay! I’ll modify the code for that, thanks! 😀

          #23082
          Jasmine Wongus
          Participant

            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. 🙁

            #23083
            Jonathan Gonzalez
            Participant

              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.

              #23084
              Jasmine Wongus
              Participant

                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!

              Viewing 6 posts - 1 through 6 (of 6 total)
              • The topic ‘Since reading the question and answer below, I tried checking to see if the array has something in it, and if it did, dest…’ is closed to new replies.