They’ve hit the Shields… Time to Show Damage on the Shields

Thomas Kesler
3 min readApr 3, 2021

--

Now that we have a fully powered up Shield, the Player is invincible… At least for a bit.

In this article, we are going to use the same principals that we used in Powering Up the Shield. If you missed that article, now would be a good time to review it.

We will once again be using a Coroutine as they are great when working with an effect that happens over a duration.

For the purposes of this Article, we are going to have the Shield hit limit set to 3 with a difference 1.5f Fresnel Power between each power level.

So with us setting up a Shield that can take damage, we are going to have to modify our Player’s Damage() method. Let’s take a look at the new code.

Player’s Damage method called when it is hit

The main difference form before is that instead of simply removing a Life, We have to wrap it in a condition. Here you can see that we are checking whether the Shield is Active and if it is not, then we can subtract a Life. Otherwise, we deal with the Shield and Start it’s Power Down Routine.

Shield Power Down Routine

This code is still rough but it does the job of taking care of the Shield when it is hit. First, We set some simple variables to work with; Our desired Power Step, the Shield’s current Power value and the target Next Power level. In the While() loop we ramp the shield power down (the higher the value, the less of the shield you see) using deltaTime until it has reached the target Next Power level.

Once that loop is down, we compensate for any overflow by setting the power to exactly the Next Power level.

Then we give the Shield a breathe before checking to see if it has been depleted, has reached 0 Shield Count. If it has this is when we turn the Active Bool to False and deactivate the Shield’s GameObject. It is within this IF statement that we could set up another While() loop to give the Shield’s shutdown a more dramatic flare than just disappearing. I think I will return to play with that another time. For now, it is time move on from the power ups and perhaps give the enemies some teeth… err lasers.

--

--

Thomas Kesler
Thomas Kesler

Written by Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.