My 90 day journey becoming a Unity game developer: Day-20
Objective: Creating the Shield Powerup and defining its behavior.

We will start by dragging the Shield Powerup sprite from the Assets folder into the Hierarchy. Set up the powerup identical to the other powerups by scaling it down in size, changing to Foreground in the sorting layer inside the Sprite Renderer, adding a Box Collider with Is Trigger selected and collider edited around the shield, adding a Rigidbody with Gravity Scale set to zero.
Now we’re going to drag our Powerup script onto the Shield Powerup. We need to change the PowerupID number from 0 to 2 which represents the Shield Powerup.

Create the animation for the shield just like the other powerups and save it in the Animations folder as well. This is how it should look when everything is done.


Drag the powerup into the Asset folder and make it a prefab, and delete it from the Hierarchy. Go to the Spawn Manager game object, drag the Shield Powerup into element #2 under Powerups to assign it as powerID #2. Now we will work on the shield’s behavior.

In the Spawn Manager script under the coroutine PowerupSpawnRoutine(), change the randomPowerup variable range from 2 powerups to 3 which includes the shield powerup now.


Inside the Player script, create a variable that will assign isShieldActive to false. Then create a method name ShieldsActive(), which will set isShieldActive to true.
Now we need to make our shield invincible for at least 1 hit. Inside the Damage() method, we will check if isShieldActive is true and if it is, do nothing.

If we use this code, this will make the player invincible infinitely. Therefore, we need to set isShieldActive to false after we take a hit to make the shield take one hit and one hit only.

Head over to the Powerup script, inside the switch condition under OnTriggerEnter(), change case 2 to the ShieldsActive() method when this ID number is called.


Now that the shield is working, let’s add visualization to the shield. In the Player script we need to create a game object variable name shieldVisualizer. Then inside the Damage() method we will deactivate the shield visualizer when the player is hit.


Go inside the ShieldsActive() method and activate the shield visualizer when the player grabs the powerup.
Back in the Unity Engine, go into the Assets folder and drag the player’s shield into the Hierarchy. Name the shield a name your comfortable with, and drag the shield onto the player making it a child of the player. Set the shield’s position to zeros which will center it to the player. Also, turn the shield off in the Inspector, we will activate it with code. Set the sorting layer to Foreground for consistency with other game objects.

Click on the player, then drag the shield visualizer onto the Shield Visualizer variable in the Player component.


We will now animate the shield visualizer. Just do the same thing setting up the shield visualizer that we did with the powerups to animate it. The only difference in this animation is that we will add some extra keys to the animation to make it look obvious the shield is animating.

This is the final result of the Shield and Shield Visualizer working.

Next we will work on adding an User Interface to this game.