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.

Also, take a look at this (forum thread) [http://forum.unity3d.com/threads/57994-Simple-Fracture-Script-v-1.01?highlight=procedural+destruction]

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 explosionRadius = 5.0;    //The starting radius of the explosion
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;
    
    //Hide your current healthy object(s)
    //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
		if (explosionRadius<18) {
				explosionRadius+=0.5;
				
				var explosionPos : Vector3 = transform.position;
				var colliders : Collider[] = Physics.OverlapSphere (explosionPos, explosionRadius);

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

				if (hit.rigidbody) {
					hit.rigidbody.AddExplosionForce(explosionPower, explosionPos, explosionRadius, 3.0);
					}
				}
		}
	} else {explosionRadius=5.0;}
}

(Not tested, but you could always get back here if something doesn’t work for you)
Waiting inside the collision function comes very handy in this case cause you could time things that should happen while the collision takes place.

I’d also go for a small emitter on every piece perhaps (if they aren’t to many), or a random selected ones which you can attach via the script if you want to make it look like fire or smoke. (Basically make an empty GameObject for it too, create an emitter and via a for-loop child them to the exploding parts at a random seed).

Good luck!

Never mind, I fond somthing else that looks better…