Upgrading My Shields

Austin Hall
4 min readOct 27, 2022

--

Adding health aspect and resetting cooldown

To start, I gave the shield powerup three hits before it disappears. It turns green on the first hit, and red on the second hit. On the third hit it goes away.

Photos taken during game play. Damage did not occur while shield was active.

The visual effect of this feature can be achieved using the UI or done through scripting. I chose to do it through scripting.

The first thing I did was create a handle to the sprite renderer component on my shield object. Since the shield object is a child of the Player, I needed to get the reference for the child object before I can get the sprite renderer component.

I assigned the sprite renderer component to my handle in the start method so that I can use it whenever I need it.

For that I used the following code…

I implemented the SpriteRenderer.color function and used the prebuilt colors in Unity.

I used if-else logic to implement the “lives” system and change the color of my shields.

Originally, I had a single if statement that turned the ‘_isShieldActive’ variable to false after it took damage the first time.

I created a shield lives variable and set it equal to 3. This is a global variable. I used the serialize field attribute to track the number of hits real time. This allowed me to verify that my code was functioning properly.

The sprite used for the shield is blue. The default color in unity is white. So when the powerup is picked up the shield defaults to the sprite color.

Inside the Damage() method, within the original if statement for the shield, I nested a series of if-else statements. Each statement checks for the number of live and then sets the shield color accordingly. I used the built in colors yellow and red for the color changes.

Overlaying yellow on the blue sprite caused it to turn green. Thus, after the first hit the shield turns green. The blue sprite just made the red look darker.

Resetting The Cooldown Timer

Something that I noticed during this evolution was that if I picked up a shield powerup while my shield was already active, my shield’s cooldown timer would not reset. So I decide to go ahead and fix that, since I was working on the shield already.

I tried several different approaches, but I ended up in the coroutine for the shield cooldown.

For the logic, I need the shield to turn off after a certain timeframe. Originally, in my coroutine, I used “yield return WaitForSeconds” and then set the _isShieldActive variable back to false. But It would not reset if it was active when I picked up the powerup.

To fix that, I created a few variables, and expanded my logic.

What I wanted to happen was that when I checked the time, I created a time stamp. I set the duration variable to 10 sec.

EDIT

In a while loop, I kept the shield active as long as the current time was less than the timestamp plus my duration. I used the function “yield return null” which basically says “do nothing”. If I picked up another powerup, it reset the timestamp.

When we exit the while loop, we set our variable and our transition parameter to false, turning off the shield.

The whole thing looks like this…

ShieldActive() is called every time the player picks up the powerup.

--

--

No responses yet