How do I make an explosion affect my gameobjects?

I have some Cubes (with rigidbodies) in my scene that I want to blow up. How do I get them to react/get affected by for example a bomb thrown in front of them? I found the following code that works nice, but I would like the cubes to be thrown up in the air as well.

var explosionTime = 1.0;
var explosionRadius = 3.0;
var explosionPower = 500.0;

function executeCollision()
{
    var colliders : Collider[] = Physics.OverlapSphere( transform.position, explosionRadius );  

    for( var hit in colliders )
    {
        if( hit.rigidbody )
            hit.rigidbody.AddExplosionForce( explosionPower, transform.position, explosionRadius );
    }
}

Collider objects = UnityEngine.Physics.OverlapSphere(explosionCenterPosition, explosionRadius);
foreach (Collider h in objects)
{
Rigidbody r = h.GetComponent();
if (r != null)
{
r.AddExplosionForce(explosionForce, explosionCenterPosition, explosionRadius);
}
}

add this code to your cub (with rigidbody) and it will give them velocity to up every mouse left click. not tested

void Update()

{

if (Input.GetButtonDown(“Fire1”)) {

rigidbody.velocity = transform.TransformDirection(new Vector3(0, 0, 5f));

}

http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddExplosionForce.html

My Two Cents:

For AddExplosionForce to work, the object you want to have flying around, must have:

  1. a rigidbody
    AND (Mandatory)
  2. a collider… simple, yet took me 30 minutes to figure out…
    Keep on Creating!

Here’s a short Youtube video that covers exactly how to explode a cube: How to Explode a Cube | Unity Tutorial - YouTube


exploding cube