x


How to Chain of Explosion?

Hello, can you please give me an example of chain of explosions? I tried with OverlapSphere's but no luck...maybe it's with OverlapSphere but I can't do it... it Creates and Destroys maximum of 2 bombs. How can I make it Explode and Destroy all objects in chain? Something like create OverlapSphere for every Destroyed object or before Destroying. And if there is another object with tag BlockBomb within range of Destroyed/Destroying object. Explode and Destroy object, and so on, or any other way. Please help, here is my code for Bomb:


using UnityEngine;

using System.Collections;

public class Block : MonoBehaviour {

public GameObject BombExplosion;
private Vector3 BlockBombPos;

void Explode(){
Instantiate(BombExplosion,BlockBombPos,BombExplosion.transform.rotation);}

void OnCollisionEnter(Collision col){
Collider[] DestroyedObjects = Physics.OverlapSphere(this.transform.position,1.5f);
foreach(Collider BlockBomb in DestroyedObjects){
if(BlockBomb.tag == "BlockBomb"){
BlockBombPos = new Vector3(BlockBomb.transform.position.x,BlockBomb.transform.position.y,BlockBomb.transform.position.z)
SendMessage("Explode",SendMessageOptions.DontRequireReceiver);
Destroy(BlockBomb.gameObject);}}}}

--- BombExplosion -> Prefab, of my explosion
range -> 1.5f

more ▼

asked Nov 15 '11 at 04:51 PM

vozochris gravatar image

vozochris
31 6 8 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

http://answers.unity3d.com/questions/160959/quotchain-of-explosionsquot-destroying-gameobjects.html

I asked this question recently and the solution that worked best was Using SendMessage to call a explosion function on the object.

A run down is, so say you have a bomb prefab, each has an overlap sphere which populates an array with all the colliders inside the spheres radius. What you want to do is attach a script to the bomb and make a destroy function that when called iterates through that array and sends the destroy function on those objects.

That should do the trick.

p.s. as SilverTabby pointed out its probably a good idea to leave a few frames between each destroy call so maybe add a few yields.

Here is my script

using UnityEngine;
using System.Collections;

public class BombScript : MonoBehaviour {

    protected Collider[] radiusCollider;

    public static float radius = 3.0f;
    private float bornTime;
    private int bombTime = 5;

    // INIT
    void Start () {
       bornTime = Time.time;
    }

    // Update is called once per frame
    void Update () {
       radiusCollider = Physics.OverlapSphere(transform.position, radius);
       bombMechanics();
    }  

    void bombMechanics(){

// This if statement is basically the bombs timer and is the second value is set in the bombTime variable 
       if((Time.time - bornTime) >= bombTime){   

//This Destroy Call destroys the bomb you dropped once the time runs out.
         Destroy(this.gameObject);

// This foreach iterates through all the colliders inside the overlap sphere and sendMessages the bombExplode method

         foreach (var bombInstance in radiusCollider) {
          bombInstance.SendMessage("bombExplode", SendMessageOptions.DontRequireReceiver);
         } 
       }
    }

    IEnumerator bombExplode(){

// after this is called it waits for a 10th of a second and then calls destroy on this object (for the sake of explanation this would be all
// the other bombs that were in the overlap sphere)

         yield return new WaitForSeconds(0.1f);
         Destroy(this.gameObject);

// this then checks all of the objects that was in its radius spheres before they were destroyed and sends the message to run this method //and so on... until all the bombs are destroyed

       foreach (var bombInstance in radiusCollider) {
          bombInstance.SendMessage("bombExplode", bombInstance, SendMessageOptions.DontRequireReceiver);
         }
    }
}
more ▼

answered Nov 16 '11 at 04:19 AM

sacredgeometry gravatar image

sacredgeometry
203 11 15 26

Urgh I cant seem to get the code block to work right...regardless here it is

p.s. I removed the bits that wouldnt be relevant and added comments so you can see what everything does.

p.p.s sorry its not in java script I work in c# I might be able to translate it if its what you need.

Nov 16 '11 at 07:17 AM sacredgeometry

lol, I'm C# too, thanks for answer I'll check if it works , if it works I'll mark answer as right.

Nov 16 '11 at 02:33 PM vozochris

Thanks, Brian 2, this really helped me.

Nov 16 '11 at 03:26 PM vozochris
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4152
x161
x26

asked: Nov 15 '11 at 04:51 PM

Seen: 702 times

Last Updated: Nov 16 '11 at 05:40 PM