I’ll break this up into 2 sections…Simple and Less Simple
For either method, all you really need to know is this secret:
There is no spoon!
There are acutally 2…or in this case, many. It is the old switcheroo which Jon Weinberger goes into detail about in his “Destructible Crate” video found at https://gamedevhq.com/
The idea is that there are 2 objects. One that is whole, and one that is in pieces. You just need to swap these out at the right time and BLAMO!
We’ll begin with the basics:
Very Simple Method:
- Add a cube to your scene
- Add a rigidbody to your cube — add gravity for more realism
- Make sure Box Collider is attached; otherwise add box collider and make sure that “Is Trigger” is NOT checked
- Scale collider so that it extends beyond the cube…don’t need to scale too much
- Create duplicates and stack them manually so that they are 4 wide x 4 deep x 4 tall (you can iterate with code or do this manually)
- If you really don’t want to code, you can keep the cube at a scale of one and hold the <Ctrl> key while moving cubes to make sure they are butted up against each other — Overlapping Colliders is KEY!
- Press “Play” and watch the havoc
Slightly Complex Method:
In order to fracture an object like I have in the title image, I used Blender which you can download for free here.
NOTE: This is NOT a Blender tutorial…
Either create your object here or import one of your choice.
Under Edit>Preferences>Add-ons, search for cell fracture and click the checkbox to add:
Make sure your object is selected and add the cell fracture Object>Quick Effects>Cell Fracture:
This gives you the original object plus a duplicate with pre-broken pieces.
From here you will need to export both objects along with any materials used into Unity.
At this point, you can either do as above by adding colliders and rigidbodies to the pieces or you can script a few lines of code.
If you chose the latter, it’s just a matter of enabling and disabling the 2 sets of objects giving the illusion they are one and the same. The solid object is used until it needs to explode where you can disable the mesh:
And enable the broken object: