Shields you can count on! or count down to.

So the next challenge is to give our shields some kind of visual indication they’re getting weaker when they’re hit. I decided on the standard changing of the alpha channel and changing the hue. At full strength, you get a nice powerful blue shield. 1 hit, and the shield goes half strength and half alpha meaning you can see through it, and we give it a magenta tint. the next hit reduces it even further to only 10 percent of its alpha and turns red. The final hit to it eliminates the shield entirely.

We already had a boolean to see if the shield was on or off, we can use that, then there’s the actual shield game object which we’ll definitely be modifying. and I created a new variable called _shieldPower which gets initialized to 3 in the the Start() function for when its first called up.

Next, I checked the powerup function and all I needed to add was the _shieldPower variable so every time it’s picked up, you get a fresh level 3 shield!

I’ll spare you the details of some of the more painful aspects of this challenge, suffice to say that originally, the shield chekcks were left in the damage section, which didn’t work because the shields wouldn’t reset properly when you gained a new power up. The function needed to be on the update loop that was checked 60 times per second for constant updating and verification of certain features.

So in Update, I created a new function called Shields(). Shields Job is just to update the look of the shield Sprite. The actual heavy work is still happening in the Damage function:

There’s a bit going on here but you only really need to focus on the first if/then check. Is the shield active? If it is, then reduce the number by 1 and leave the function. This basically means the ship will hit the player but it won’t reduce the _playerLives variable because the shield is…well…shielding it until it runs out of power by going to 0. When _isShieldActive turns false, then it will ignore everything inside the function and move on to the next line which is take a Player life.

Back to the Shields function which is called in Update. I put in a switch case since it’s a perfect fit for this situation:

switch(shield power)

In case it’s level 3,

just leave the alpha at 1, and remind the game the shield is still active.

In case of level 2,

change the alpha to .5 and tint the shield magenta. remind the game the shields still active.

in case of level 1,

change the alpha to .10 percent, tint its color to red, remind the game the shields still works.

and finally when it reaches 0,

tell the game the shield is no longer active and deactivate it.

Pretty straightforward, right? Now we just need to translate it into something Unity understands:

To change the shields color, we need to make a temporary variable for the shields SpriteRenderer. Then we can make our alpha changes to that and assign it to the current sprite renderer. In the first case, I use white as kind of a ‘palette cleanser’ to remove the other colors when it resets. As you can see there’s almost no difference in the description to the actual execution in code. Just some general rulesets as to how you tell Unity to behave and it’s pretty easy to figure out! It’s a little redundant, but the code works! Let’s see it in action:

Looks good! Tomorrow we’ll get into limiting our ammo!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store