Adding Damage for the Propane Tank Explosions

GameDev Dustin
5 min readJul 6, 2022

In this article, we’ll make it so that our exploding propane tanks are more than just visuals.

They will damage any nearby Robot AIs.

For each propane tank prefab, we need to add an empty game object child with a box collider.

We’ll size this to represent the area where damage will be inflicted.

Once we’re done, we should be able to select the propane tanks in the scene and see something similar to the above.

We’ll need to know which RobotAI game objects are in the damage collider when the tank explodes, so we add a variable for this to the PropaneTank class / script.

We can then assign this collider to our script component for each tank prefab.

If we look at the tank game objects in our scene, we can see that these changes to the prefabs have carried over nicely.

Issues With Hit Detection
At this point testing the game showed that hit detection failed.

That’s because I’ve created large colliders that block the other colliders that would normally detect hits, such as cover, the AI, and even the actual tank itself.

Solution
I’ll reduce the height of my damage zone colliders so that it will detect the AI’s feet and not block other colliders.

Do this for each tank prefab.

This solution works good for tanks sitting on the floor, but as you can see with the small tank on top of a crate, now much of the cover is blocked by this danger zone collider.

We’ll have to modify each collider individually, which is what I was trying to avoid.

I’ve revised each game object’s DamageZone collider to look similar to that above.

We’ll need to keep track of which AI game objects are in the damage zone so that we can iterate over them when it explodes.

To do this, we’ll create a new script and add it to the DamageZone game object on each of our tank prefabs.

We’ll keep track of the Robot AI transforms by detecting their HitColliders as shown above.

If we run the game and pay attention to the DangerZone game object attached to our first propane tank in the scene, we can see the array is updating correctly.

Now we can add a public method that will iterate through, determine the parent game object, and call TakeDamage() on the parent game object’s RobotAI script component.

We can then add a UnityEvent for doing damage in our zone to the PropaneTank class / script and invoke it from the Explode() method.

For each propane tank prefab, we assign the DamageZone game object and call its DoDamage() method in the Unity Editor.

We can then check the actual propane tank game objects in the Scene Hierarchy and make sure that the UnityEvent is correctly assigned from our prefabs.

Before we get to testing this, I’ve updated the script with a _doDamagPhase Boolean variable.

Without this modification, only those robots in the trigger zone at the time of the explosion will die, but those that run into the explosion for the next 3.5 seconds or so were unaffected.

Now, they will also take damage.

The PropaneTank script also needed to be updated with a Boolean value for when it is in the process of exploding.

Otherwise, continued hits from the player would call for it to explode repeatedly.

I also had to add an isDying Boolean class variable to the RobotAI script.

The Start() method sets it to false, the OnDeath() method sets it to true, and the new IsDying() method returns the current value of _isDying.

This way, while the Robot AI is playing its death animation, it isn’t still being told to take more damage.

At least it won’t once we update the DamageZone script.

With a little modification of the DoDamage() method in the DamageZone script, we can check if the currentAI selected is already dying.

I kept getting null reference exceptions until I refactored my DamageZone script’s DoDamage() method to null check the transform in the foreach loop.

Due to the issues experienced, this article ended up jumping around the code a bit more than I’d like.

Here is screenshot of the finished working DamageZone class / script for reference.

And here is the final PropaneTank class / script.

And there we go, it works!

--

--