Unity Explosions

Hal Brooks
3 min readMar 12, 2022

Objective: Create an exploding crate that knocks back objects.

Create a new scene, and import the Breakable_Wooden_Crate and Explosion_09 from GameDevHQ Filebase. The Breakable_Wooden_Crate has two fbx child objects, Cube_Cube_001 and Voroni_Fracture, which need to be separated from their parent prefab so they can be independently instantiated.

Breakable_Wooden_Crate children.

Drag the Breakable_Wooden_Crate into the hierarchy then unpack the prefab. Move Cube_Cube_001 into the root hierarchy for the scene and rename it Crate. Similarly move Voroni_Fracture into the hierarchy root and rename it Fractured_Crate. This Fractured_Crate has 22 pieces of the crate each of which needs a rigidbody and a mesh collider with convex checked.

Regidbody and Mesh Collider required for crate fragments.

Drag the Fractured_Crate into the prefab folder. Delete Breakable_Wooden_Crate and Fractured_Crate from the hierarchy window. Add three cylinders positioned roughly 3, 7 and 10 meters away from the crate. Add a rigid body to all three cylinders.

Fractured_Crate pieces outlined in orange.

Create a new script named Controller which will be used to trigger the explosion and attach it to the Main Camera. This script will need two floats, one for the explosion _fore and another for its _radius. References to three GameObjects are also needed, _crate which will explode, the _brokenCrate, and the _fireball particle effect.

All five variables use SerializeField and are assigned in the inspector as shown below.

Variable assignment within Controller script on Main Camera.

Within the void Update() method the user input is checked and if the space key is depressed then the _brokenCrate with its 22 fragments is instantiated followed by the _fireball at the _crate’s position. The _crate is then destroyed to prevent spawning additional fireballs, and the Explosion() script is called.

Triggering the explosion with space key within void Update() in Controller script.

The Explosion() script uses Physics.OverlapSphere to find all colliders within the _radius of the explosion. This finds not onlyt the fragments within the _brokenCrate, but also the cylinders. If any pieces are found then TryGetComponent is used on each piece to get the rigidbody, body, which if found then AddExplosionForce applies the _force to the object based upon the _crate’s position and the _radius of the explosion, defined earlier.

Explosion method within Controller script.

The _force decreases with distance from the _crate.transform.position. Looking below the rear cylinder, which is 10 meters away, is unaffected by a blast with _radius eight. The right cylinder, just 3 meters from the crate, is pushed further than the left cylinder, at 7 meters, due to the falloff of force with distance for the origin of the explosion.

To get Explosion_09 to initiate immediately on instantiation set Start Delay to zero and set Time for Bursts within the Emission to zero, otherwise particle animation will be delayed.

Make sure delays are zero in particle effects for explosion.