Adding the Player Shield

Game functionality

Gert Coppens
Nerd For Tech
3 min readAug 27, 2021

--

Almost identical to how we set up the Player Health, Player Health Bar and Health Power Ups, in this article we’ll look at introducing a Player Shield System as well as linking it to a Shield Bar UI Element, and creating new Shield Power ups.

Creating the Player Shield System

To start we create a few new variables to initialise the Player Shield and to check if it’s active or not:

in the Player.cs

In Start() we set the current shield to the max shield:

current shield = 100 at start

Next we create a method allowing us to damage the Shield:

Similar to the TakeDamage() Method

Additionally in Update(), we execute another method responsible for deactivating the Shield when it depletes and in general for Shield behaviour outside of the DamageShield() method:

Implementation

Now that we have a shield system in place we can basically mitigate the shield damage into normal damage for as long as the shield is active.

As soon as the shield is deactivated, it’ll continue to execute its logic.

Creating the Shield Bar

To create the Shield Bar I just duplicated the Health Bar, renamed it and inverted it on the x-axis. I set the fill Image color to blue.

008AFF

In the UI Manager.cs, very similar to the Health Bar, we create two new methods (having a reference for it just like we have for the Health Bar); one allowing us to set the initial shield, and one to update the current shield. This time we wont be using a custom gradient as I don’t think it’s necessary for the Shield.

Having our methods updated the Shield Bar should now be functional.

Creating Shield Power ups

Likewise very similar to how we created Health Power ups, we can now manufacture the logic for Shield Power Ups.

Power up logic

Then we have to update our PowerUp Enum and the OnTriggerEnter() method in the Item.cs Script:

Now we can create three new prefabs that we can spawn:

In the following article we’ll be improving the Player Speed Boost, create a visual UI Element for it and add Speed Boost Power Ups to the Game.

Previous | Next

--

--

Gert Coppens
Nerd For Tech

Software Engineer — Unity Game and Application Developer