Creating a Shield Powerup — Part 1, The Pickup
In the last article we created some Animations for our previous Powerup collectibles. I think it’s time for another one!
The difference in this powerup will be that we’ll connect a visual shield to our Player to indicate that our Shield is activated!
But first we’ll create the Pickup to activate the shield and give it functionality!
Creating the Powerup collectible
As done previously, we’ll drag in a Single Shield powerup sprite into the Scene Hierarchy to create the base of our Shield Powerup object. Then add our Rigidbody 2D, Box Collider 2D, and our Powerup Script.
- Remember to set the Rigidbody 2D Gravity Scale to 0.
- Turn on Is Trigger on the Box Collider 2D, and edit the collider to fit the sprite.
- Within the Powerup Script we’ll change the Powerup ID to be 2.
Then we’ll create the Animation (refer back to the previous article).
And then make your new Powerup a Prefab!
Organize the Animations
We now are beginning to have a lot of Animations and Animator Controllers. It’s best that we put them all into an Animation Folder such as this:
Sometimes, when moving animations and controller, you may need to relink them to your GameObject. Do this if you find your animations not working after moving them into a folder.
Fix the Spawn Manager script
Now that we have our Shield Powerup Prefabbed, we’ll let the Spawn Manager know that it can use it.
On the Spawn Manager object, in the Spawn Manager script component, we’ll change the Size of the powerup array to 3, and drag the Shield collectible into the 3rd slot:
And inside the Script we’ll change our randomPowerup variable to include the Shield by incrementing it to 3 max value.
You could also use Random.Range(0, powerupsArray.Length) which would give you the same result. I will be changing my script in the future once I add more powerups.
Creating the Shield Logic
The Shield collectible spawns properly now, but when we interact with it, nothing happens! So let’s give it some logic.
Inside the Player script we’ll create a private bool _isShieldActive and set the default value to false. This will let our Player class know to utilize the Shield or not, initially not being used until the Powerup is collected.
Then we’ll create two public functions to activate/deactivate the shield.
Now we’ll jump into our Powerup Script to first activate the Shield when the powerup is collected:
And now back into our Player script to finish off our logic.
We want the shield to be called during the DamagePlayer() function, so long as the player shield has collected the powerup and the shield is active. If so, then our shield will deactivate, and our Player will not lose a life.
Our Shield should now be working! We don’t have a visual representation yet, but the logic is sound and can be checked in our Inspector when play-testing the game. Watch the lives on the right and notice how they do not decrease after collecting the Powerup!
In the next article we’ll give our Player an actual Shield so that we know when the shield is active or not!