Level Up Your Unity Skills: RED ALERT! Increase Power to Shields

Gerald "Ray" Patton
3 min readMay 5, 2024

--

Objective: Develop an Shield that gives extra protection with a visual representation of Shields failing.

Designing and developing this 2D Space Shooter I noticed the shields seem to be a little weak. So I decided to tweak the shields to add more strength or _shieldHealth and make the _shieldVisualizer within the Player Script to display this.

I decided to make the shields move from Blue, then Green, then finally to Red before destroying. But to begin with I created a Serialize Field variable for the _shieldHealth:

The _shieldHealth will be adjusted within the script. To do this I need to modify the public void Damage() method within the Player Script. So, I took this part of the code:

And tweaked it adding the new _shieldHealth variable:

This code says, that as long as the _shieldHealth is greater than 1(one) the shields will remain up. If the _shieldHealth drops below 1(one) the shields power down.

When I test this out, the _shieldHealth addition to the public void Damage() method worked. But know I just need to add the color representation. As mention earlier in this article I intend to make the Shields change color, starting first with Blue, then Green, then finally Red before becoming disabled from a final hit to the shield.

To do this switch, will require a SpriteRenderer.color method within a switch statement. So similar to the switch statement I used with the Powerup Script. I created another switch statement within the Player Script:

To find the color values for the new Color component, I opened the Color field within the Sprite Renderer on the Shields within the Inspector. I then change the RGB 0–255 to RGB 0–1.0

From here I use a combination of the color wheel and color square to find the tint I wanted to use on my Sprite Renderer. It is important to note that this process does not actually change the color the the Sprite but applies a tint over the Sprite. For example if you have a Sprite that starts with a blue color, like the shield Sprite and you apply a yellow tint to that Sprite, you will not get yellow. You get green, because as you may have learned in primary school art, added yellow and blue paint together get green. If you truly want the Sprite to reflex the tint color from the Color wheel, the Sprite needs to start as a greyscale.

After, code the _shieldHealth switch statement, I ran a test. Everything tested according to what was expected. Until, next time. Happy Developing!

--

--

Gerald "Ray" Patton

Unity Developer | C# Software Engineer | Game Developer | USAF Veteran