Phase 1: Shield Strength

Dominique Dos Santos
4 min readApr 29, 2021

--

Yesterday’s challenge was a “walk in the park” and I managed to implement what was required without much issue but today’s challenge is a bit harder to complete.

Visualize the strength of the shield. This can be done through UI on the screen or colour changing of the shield.
Allow for three hits on the shield to accommodate visualization

Currently, our shield only allows for one free hit without losing a life. Adding this functionality isn’t too hard but before we get into the “guts” of this feature I’ve thought of a few important things to consider before continuing:

  • I’ve opted to create a shield status on the UI with 3 bars to update how many hits we have.
  • If the shield is at max “charge” and another shield powerup comes along, then nothing should happen.
  • If the shield “charge” is less than the max (3) then getting another powerup will regain one charge.

Since My game didn’t have much in the form of a UI I decided to look for some assets and the Space Expansion UI pack from kenney.nl seemed like the perfect fit. I’ve downloaded, imported and sliced the sprites and added some elements to my Hierarchy.

Coding the logic to update the UI is a bit of a complex system which we’ll cover shortly, I wanted to first code the shield logic to ensure everything works as intended. For testing purposes the use of ‘Debug.Log(“LogMessagehere”);’ is an invaluable tool. I’ll be dividing the logic into a few sections and explain each section as we go along. But before I dive too deep into the actual logic of the shield, I figured I’d start by creating a new function in our UIManager that will update during various stages and functions in our player script.

UIManager Update Shield Visuals

In our UIManager I created a new section where our variables are nested and added an Image array called “_shieldIMG” and created two Sprite variables called “_shieldActiveImg” and “_shieldInactiveIMG”. This allows me to set the image of each visual object of the shield to show a nice white colour when the shield is active and displaying how many “hits” you can take and then displaying the removed points with a semi-transparent “shadow” image.

I created a public void function called UpdateShield and added a variable that we’ll be using to add a value from our player script to update the correct image. I’m using a for loop to update the individual images in the array by comparing that to the remaining shield health and then changing the image of the corresponding health value. I had to also change it back when we get a new shield powerup or if we get one point back again.

Player Shield Setting Changes

Since the shield logic mostly exists in our player I figured to start by adding the new shield settings to my list of defined variables. Under my shield GameObject and bool settings, I created an int and called it “_shieldHealth”. I didn’t assign a value to the shield health since the game would start with no shield, and then when the users get a shield, it will be updated.

Player Damage Function Updates

In our Damage function, I already have an if statement to check if the _shieldActive is set to true, and if that is the case it will disable the shield, and break out of the function as not to damage the player. We’ll be making minor changes to incorporate shield health, and minus one point from the health of the shield is active, and lastly, if the shield health is 0 then the shield should be disabled.

Player Powerup Function Updates

Next, we need to make some tweaks to the powerup function. Remember earlier I mentioned that if the player gets a shield and another shield powerup comes along we should check if the shield is at max (3) or below then determine the correct action to take.

Wrapping Up

This was quite a challenge and took me a few tries to get this to work properly. At times the images were inverted or not updating the correct image and even getting out of bounds exceptions but by tweaking things I got it to work. It could’ve been much easier just to update some text element but where’s the challenge in that… Pun intended.

--

--

Dominique Dos Santos
Dominique Dos Santos

Written by Dominique Dos Santos

Self-taught Unity and C# Developer with a passion for games and the stories they tell.

No responses yet