Okay, I know the title seems a little confusing, but what I’m trying to achieve is a simple concept… Say I have a bomb, called Bomb0, and there is another bomb, Bomb1, right next to Bomb0. When Bomb0 explodes, how would I trigger Bomb1 to explode, since it is within the original explosions radius? For some reason the following script just lags Unity out to where I have to close the application.
Here is the code:
using UnityEngine;
using System.Collections;
public class Explosive : MonoBehaviour {
public GameObject ExplosionPrefab;
public float ExplosionRadius = 10f;
public float ExplosionForce = 100f;
public float UpwardsModifier = 15f;
public LayerMask mask;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision other)
{
if(other.transform.tag != "Terrain" other.transform.tag != "Explosive" ){
Explode();
Destroy(this.gameObject);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, ExplosionRadius);
}
public void Explode()
{
Debug.Log("Exploding " + this.gameObject.name);
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
//Debug.Log("Hit " + other.transform.name);
Collider[] colliders = Physics.OverlapSphere(transform.position, ExplosionRadius, mask.value);
foreach(Collider col in colliders)
{
if(col != this.gameObject.collider){
Debug.Log("Explosion reached " + col.transform.name);
col.rigidbody.AddExplosionForce(ExplosionForce, transform.position, ExplosionRadius, UpwardsModifier);
}
if(col.transform.tag == "Explosive")
{
Debug.Log("hit another explosive!");
col.gameObject.GetComponent<Explosive>().Explode();
}
}
//Destroy(this.gameObject);
}
}