Shield Strength! — Fixed!
Objective: To solve the problems in our code that led to unwanted Shield behavior in yesterday’s article
Yesterday was a bit of a doozy, wasn’t it? The video below is what we want our Shield to do.
…we ran into the above issues. Our Shield did not return to its full, blue Color when we picked up a second powerup, and it also seemed to regain one hit point after running into an Enemy (because it didn’t disappear). In addition, our Shield turns to a green Color after being hit once, not yellow. Definitely not what we want!
Let’s start with the issue of the Shield being green instead of yellow first. We can verify in our Inspector that the Shield did, in fact, become yellow!
So why did it appear green in the game? The issue is that the original Shield sprite was created using a blue Color. When we changed the Color to yellow in our Inspector, we didn’t actually change the Color; we changed the tint. We basically added a yellow tint to the original blue Color, causing the Colors to mix and make green! Kinda neat, but how do we fix that? The short answer is that we can’t. Well, at least not without importing it into other software to modify it. So for now, I think I’ll add a gray tint to make it a more subdued blue after getting hit once.
Next, let’s look at the issue of our Shield not turning back to its original blue when we pick up a new powerup. Picking up a Shield powerup calls our ShieldsEnabled method, but we failed to add anything related to our Shield’s Color component. Let’s fix that now.
You’ll notice we added two things here: the first is that we set the Shield Color to white, and the second is that we set our _shieldStrength to 2. The reason we set the Color to white is because white represents the default Color. Our Shield sprite’s default Color is light blue, so setting it to white actually changes it to light blue (if we set the Color to blue, it would just be adding a blue tint on top of our original blue Color, making a deeper blue).
white → default ← original blue Color
And setting the _shieldStrength to 2 means we will start over in our switch statement when we pick up another Shield powerup, regardless of how many hits we had left on our Shield before we picked it up.
Shall we test it out in Unity again?
Alright! It looks like this time, we got everything to work properly! There is a lot of customization that we can do with our Shield besides Color, such as making the Shield smaller as it takes hits, adding a flicker effect, etc. Feel free to play around and try other effects, but that’s all we’ll be doing on Shields for now! Tomorrow, let’s dive into adding an Ammo count system! Hope to see you there!