# exploding game object to pieces

I want to make a game obget that made up of many little game objects that explode to the pieces that it made up of. When somthing collide with it the game object destroys but not the the pieces it made up of. How do I make it explode to pieces? every game object have rigidbody.

Sorry for poor english.

Your mileage may vary- I found it worked relatively well as long as you’re not fussy about how broken the pieces are. Some fiddling required to prevent the physics going haywire.

You could make a prefab out of the pieces put together then when something hits your object you switch the healthy parts to the pieces. This is also very handy cause then you can set a different texture to the broken parts. Create an empty GameObject in your scene and put the pieces together inside it, then make a prefab out of it.

To your healthy GameObject attach this script:

``````var brokenPartsPrefab : Transform;    //Your prefab of broken parts
var objectExplode : boolean = false;    //Activates the explosion force later
private var currentPos = Vector3;    //Your transform's current position
private var currentRot = Quaternion.identity;    //Your transform's current rotation

private var explosionPower = 10.0;    //The power of the explosion

function OnCollisionEnter(collision : Collision) {

//Grab the magnitude of the collision
var collisionMagnitude = collision.relativeVelocity.magnitude;

//Only execute if the magnitude is at a certain level (You could of course change this to a health instead that gets less for each hit, when health==0 then execute)
if(collision.relativeVelocity.magnitude > 10){

//Inactivate the collider for this object
rigidbody.detectCollisions = false;

//Store the current position and rotation
currentPos = transform.position;
currentRot = transform.rotation;

//Change the "YourGameObject" to the real name of your GameObject (This is if your GameObject contains children)
var healthyParts = [];
healthyParts = GameObject.Find("YourGameObject").GetComponentsInChildren(Renderer);
for(var rChild : Renderer in healthyParts) {
rChild.enabled = false;
}

//Instantiate the broken parts at this transform's position and rotation
Instantiate(brokenPartsPrefab, transform.position, transform.rotation);

//If your object is moving give some of the force to the instantiated rigidbodies
var brokenParts = [];
brokenParts = GameObject.Find("YourBrokenPartsName(Clone)").GetComponentsInChildren(Rigidbody);
for(var rbChild : Rigidbody in brokenParts) {
rbChild.velocity = rigidbody.velocity/2;
rbChild.angularVelocity = rigidbody.angularVelocity;
}

//Activate the explosion
objectExplode = true;

//Wait given amount of time to remove the instantiated gameobjects and reset everything
yield WaitForSeconds (5);
Destroy (GameObject.Find("YourBrokenPartsName(Clone)"));
rigidbody.detectCollisions = true;

for(var rChild : Renderer in healthyParts) {
rChild.enabled = true;
}

transform.position = currentPos;
transform.rotation = currentRot;

objectExplode = false;
}
}

//For a great visual effect explode the pieces apart from eachother
function FixedUpdate() {
if (objectExplode == true) {

// Apply an explosion force to all nearby rigidbodies

var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, explosionRadius);

for (var hit : Collider in colliders) {
if (!hit)
continue;

if (hit.rigidbody) {
}
}
}