Day 30 — Shields Up!
Today, I’m implementing my Shield Power up.
Objective: Implement the functionality of the shield (not losing a life when colliding with enemy) and then we will activate the visuals as well as animate it.
First, thing was pull from my sprite folder the first sprite fromt the shields folder onto the hiearchy. This will place the shield image in the scene. From there, I renamed it “Shields” in the inspector, followed by un-checking the little box right next to where we changed the name. This will deactivate it, and remove it from the screen.
Next, in the hiearchy, we need to click and hold the shield, and hover it over our “Player” to make it a child of the player. You’ll know you’ve done it write when an arrow appears to the left of Player in the Hierarchy and and you can click it to show the child GameObject.
Speaking of, now we go into our Player script to start dealing with the behavior. First, let’s create the private variable.
Again, we only serialize it for debugging purposes within the inspector.
After, we now create a check to see if our shield is active or not. We create a public method called ShieldActive() and create an if statement.
Then, lastly, we move back on up to our Damage method. In there, we will create another if-statement.
In this statement, we’re saying that if the shield is active, if the player is damaged, the shield will no longer be active (and return to false) and then the method returns, which means that the program flow returns to where the method was called from.
Be sure to test it. You should be able to go into the Unity editor, play the game, and upon collecting the shield power up, you won’t lose a life. You can see this play out in the Inspector every step of the way.
Visualization
Objective: We need to make it so when we activate the shield power up the visualizer shows up so we know it’s active and when we take a hit it goes away to show that it isn’t active anymore.
First we need to add our shield visualizer into the hierarchy and you should make it the child of the player. Once, it is the child of the player game object you want to deactivate it by pressing the check mark beside the name in the inspector.
After you’ve got everything set up we want to go into our player script and add a new game object variable for the shield visualizer. Then go down to your shield active method and you will want to add the shield visualizer variable you just made and set it as active.
Next you’ll want to go to your if statement that handles your shield power up in your damage method and do the same but in the brackets of the set active you’ll want to write false to set the visualizer as inactive.
Lastly, you should test to make sure the visualizer shows up when you get the shield power up and it disappears when you take a hit from an enemy.
Viola!
Cheers! See you tomorrow.