top of page

Destructible Tool Asset

The Components

There are 3 components that need to be setup by this tool:

1) The undestroyed asset

2) A prefab of the destroyed asset

3) The destructible manager 

1) The undestroyed asset

Unity_xDeBeiBrDi.png

2) Prefab of the destroyed asset

A prefab needs to be created for each 'destroyed' version of an asset. Luckily the tool does all the heavy lifting here, minus some optional parameters a user can assign.

In the prefab, all the pre-broken 'chunks' are children objects of the main parent. The only thing the user needs to do is add the 'Destructible Object Setup' script onto the parent, and hit 'Populate children components'. They may choose also to adjust the size of the spawned sphere colliders.

The script does the following:

-Adds a 'Destroyed Object' component to the same parent object

-on each child object, checks for a 'mesh renderer' component

-if a mesh renderer is found (meaning this object is a valid mesh), adds a sphere collider, destroyed chunk, and rigidbody component.

No further setup is required on this prefab, unless the user wants to modify the behavior of the spawned chunks by modifying some parameters on the 'Destroyed Object' component. (examples of variations below)

Unity_iixvSMRFHO.png
Unity_aXajAkQHUm.png

Below are some variations of the Destroyed Objects parameter. It's important to note that since this is using physics, the way the original object is hit will vary how the chunks scatter when destroyed. All the below gifs represent a bullet flying through the object in exactly the same way to illustrate the variation.

example01.gif
example02.gif
example03.gif
example05.gif
example06.gif

3) Destructible Manager

devenv_xXi41opYlj.png

I felt it necessary to add a singleton manager, for a couple reasons. First, it allowed me to make a simple but effective pool system, and Second, I could have a single entity that can be interacted with when an object needs to be destroyed, instead of having similar code potentially scattered around different classes.

One other feature this allowed me to add is the ability to filter layers to check to see if a valid collision wants to destruct.

Below is the chunk of code in this manager that handles the pooling, and actually spawns the destroyed chunks. Pooling is useful, because it allows this class to reuse already spawned objects if possible, which can potentially save some overhead.

bottom of page