How to create breakable crate box in Unity

Hello friends,

I am new to Unity and I want to create a breakable wooden box in unity, I mean some item is inside of the box if player kick that box it will break, then player can take that item :o

please help me…

Thank You.

regards[/b]

you can just make the breaking an animation and if the player kicks the box, play the animation.

Thank you very much for your tip, its useful but I want to create with the PhysX :slight_smile:

There are no breakable objects in Unity.

So you would either have a box that is already made up from parts and have those parts sitting idle until kicked (but then the box can not be affected by physics until beeing kicked).
Alternatively you would have a box thats beeing replaced with the breakable one when its beeing kicked.

Thank you for your quick replay, I will try this :slight_smile:

There is a script in the FPS tutorial called damage receiver. This would work for what you want I think.

You’ll need two models, one normal box and one busted one. Attach this script to the normal one in your scene.

var hitPoints = 100.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody;

function ApplyDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
		return;
		
	hitPoints -= damage;
	if (hitPoints <= 0.0) {
		// Start emitting particles
		var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
		if (emitter)
			emitter.emit = true;

		Invoke("DelayedDetonate", detonationDelay);
	}
}

function DelayedDetonate () {
	BroadcastMessage ("Detonate");
}

function Detonate () {
	// Destroy ourselves
	Destroy(gameObject);

	// Create the explosion
	if (explosion)
		Instantiate (explosion, transform.position, transform.rotation);

	// If we have a dead barrel then replace ourselves with it!
	if (deadReplacement) {
		var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

		// For better effect we assign the same velocity to the exploded barrel
		dead.rigidbody.velocity = rigidbody.velocity;
		dead.angularVelocity = rigidbody.angularVelocity;
	}
	
	// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
	// right away
	var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
	if (emitter) {
		emitter.emit = false;
		emitter.transform.parent = null;
	}
}

// We require the barrel to be a rigidbody, so that it can do nice physics
@script RequireComponent (Rigidbody)

In the inspector you will see Dead Replacement, drag the busted box over that and you’ll have what you want. Destroy the first box and the busted one will appear at the exact location the undamaged box was at.

You might want to add some particle effect or something to simulate the box being broken, some splinters or something maybe?

Nice stuff Trickyspark.

I’m, going to have to try that on something.

Yep it’s nice.

Thank whoever wrote the FPS tutorial. There’s alot of good stuff in there you can tweak to suit your needs.

You can make breakable objects in Unity. Try using joints to do so. It takes some experimenting, but you could get it right. I have made some breakable tables doing this.

What I would do is take your model and break it up into its smaller broken pieces. Then save all the pieces as separate objects. Then you need a script that will spawn all the pieces and apply the force from the main object to them and delete the main object. So the script on the main object you be:

//Sudo Code
if (health == 0)
{
piece_1 = new object at proper xyz coords to match original object.

piece_2 = new object at proper xyz coords to match original object.
...

peice1 add force from parent object

peice2 add force from parent object
...

//Possibly insert particle creation code here

destroy parent object.
}