- Home /
Explosion triggers another explosion
hey guys , i think this concept have never been mentioned ,so what i want to achieve is :
°there's box (1)
°and there's Box(2) near box(1)
--is it possible to ( when box(1) explodes , the explosions will affect box(2) and make it explode too )
is this possible ? thanks !
What do you mean by explode? The box dissapears and some particles shows up? You can make yield, you can make a trigger, that animated inflation (Scale) with certain speed, so when it reaches the other box it activates it. There are lot's of possible ways to achieve that.
hey @instruct9r thanks for the reply ,yeah exactly in my case , the box gets destroyed and some particles show up , yeah i taught about triggers , can we use particles ? like if particles touches Box(2) , box(2) will get destroyed ?
Hi Adamchakov, I have being trying to achieve it but with no success. Can You please help me if you can. I have stoped building my game because of this situation. I want to explode a box and it triggers other explosions near by. thanks
Answer by MisterMike · Feb 25, 2014 at 01:10 AM
If you're talking about causing a chain-reaction one way to do it would be to do a Physics.SphereCastAll against a layer holding all explosive objects once an object detonates to see if any are in range to be affected.
This answer is what I did to make something like what the question is asking about - a chain of explosions like what you get with bomb flowers in Legend of Zelda: Ocarina of Time.
For a little more detail, you'd make, for example, a class called ExploderClass, and in C# do:
using UnityEngine;
using System.Collections;
public class ExploderClass : $$anonymous$$onoBehaviour {
//used with Invoke() to cause chained explosions
//to happen one after the other, rather than
//all simultaneously
private const int ExplosionDelay = 0.1f;
private const float ExplosionRadius = 2f;
public Layer$$anonymous$$ask layer$$anonymous$$ask; //set this to whatever layers you want to get hit by the explosion
//causes explosion to happen
public void Explode() {
//get all colliders this explosion will hit
Collider[] colliders = Physics.OverlapSphere(transform.position,ExplosionRadius,layer$$anonymous$$ask.value);
foreach (Collider c in colliders) {
//get the ExploderClass script attached to the
//other collider, if any
ExploderClass exploder = c.GetComponent<ExploderClass>();
//call the other exploder's Explode() method to get
//called after ExplosionDelay seconds. (Note: if
//no ExploderClass script was found on c's
//GameObject, this if statement doesn't run)
if (exploder) exploder.Invoke("Explode",ExplosionDelay);
}
//Destroy this GameObject so it doesn't get re-exploded
//by another exploder
Destroy(gameObject);
}
}
Edit: fixed code formatting, added some more variables and using statements as needed, replaced SphereCastAll() with OverlapSphere()
hey @nesis thanks for the help but your script has errors , can u please test it =( ?
hey @nesis thanks a lot for editing the script , but where should i add this script , i added it to an object with a sphere collider , when i destroy the object , well nothing else that was inside the sphere gets destroyed , nothing was destroyed , thanks hope you'll reply ASAP
Answer by kyrnal · Feb 25, 2014 at 01:11 AM
You could use a raycast from the first box to trigger the second box.
Physics.Raycast - Documentation
Also you can use the Vector3 shorthand to cast rays in many directions.
hey @kyrnal thanks for the reply , tell me please , how can i use this script to destroy all the object tagged as "TNT" that are located inside this 100 meters ? thanks !
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}
// Note : the Integer "10" in the Physics.SphereCastAll line refers to the int value of the TNT layer.
function Update () {
var hit : RaycastHit;
var hitColliders : Collider[];
hitColliders = Physics.SphereCastAll (transform.position, 100.0, hit, 10);
for (var objCollider : Collider in hitColliders) {
objCollider.root.gameObject.SetActive(false);
}
}
I didn't test that, so it might error. But it could point you in the right direction, perhaps?
$$anonymous$$ister$$anonymous$$ike had the right idea with SphereCastAll
hey @$$anonymous$$yrnal thanks for the help but nop didn't work :/ it has an error : No appropriate version of 'UnityEngine.Physics.SphereCastAll' for the argument list '(UnityEngine.Vector3, float, UnityEngine.RaycastHit, int)' was found.
Your answer
Follow this Question
Related Questions
Player object still gets destroyed even when shields up 1 Answer
If statement not working inside switch statement 1 Answer
Trigger explosion on rocket 1 Answer
Area of Effect damage trigger 2 Answers
How to trigger a particle effect? 1 Answer