2D Galaxy Shooter — Shield Powerup and Adding an array to the Spawn_Manager
Hello all!
Today, I will be going over how to make a Shield powerup using a shield sprite over the player and the powerup sprite, then add the powerups to the Spawn_Manager by using an “array”.
Making the Shield Powerup Sprite
Similar to the Triple_Shot and Speed_Boost, let’s make the Shield sprite.
- Find a good .PNG sprite that represents a Shield, add it to the hierarchy, and rename it “Shield_Powerup”.
- Add a Box Collider component, check “Is Trigger”, and make the hitbox.
- Add a Rigid Body component and change the “gravity scale” from 1 to 0.
- Add the “Powerups” script
- Set the Sorting Layer to Foreground.
- Make it a prefab by dragging it into the prefabs folder.
- If you do not have the assets to animate the sprite, go to the next section, and if you do, continue.
- Go to the Animation window, create an animation file named “Shield_Powerup_anim.anim” in the Animation folder.
- Hit the record button on the top left of the animation window.
- Now drag the pictures into the DopeSheet and stop recording.
- Hit the play button to test it out.
- Then override the Animation component to our prefab.
Making the Shield Sprite that Covers the Player
- Find a Shield sprite that is kind of transparent and can cover the player easily, drag it into the hierarchy, then name it “Shield”.
- Make it a child of the Player by dragging it over the Player in the hierarchy.
- This allows the Shield to be activated, deactivated, and follow the player. - Set the Sorting Layer to Foreground, and set its value to 1 so looks like the player is inside the shield
- If you do not have the assets to animate the sprite, go to the next section, and if you do, continue.
- Go to the Animation window, create an animation file named “Shield_Visualizer_anim.anim” in the Animation folder.
- Hit the record button on the top left of the animation window.
- Now drag the pictures into the DopeSheet and stop recording.
- Hit the play button to test it out.
- At the top of the inspector on the top left, there is a checkbox next to its name, deactivate that. When we collect the powerup, it will activate again.
Behavior of the Shield
All the shield is going to do is negate 1 damage when hit while active.
Head to the Player script and add variables —
And make a new method, Unlike the last two powerups, we do not need a cooldown time for the shield. It is active until hit.
- _shieldVisualizer.SetActive(true) will turn on the shield sprite around the player.
Now head to the Powerup script and head to the switch statement for powerups.
Now that we can activate the _isShieldActive to true. Let’s head back to the Player script and find our Damage method.
When we add that if statement, if the shield is active and we get damaged, then turn off the shield, turn off the visualizer, then return.
- Return means that if you reach the return line, anything below that line won’t be called in the current method. This means that if the shield is active, then only the if statement will be called, and if the shield is not active, then we damage the player like normal.
Test out the shield by adding the powerup to the hierarchy, and hitting an enemy while watching your lives.
Implementing Powerups into the Spawn_Manager script
- Head to the SpawnManager script
- Delete variable “_tripleShotPowerupPrefab”, we won’t need it anymore.
- Add variable — [SerializeField] private GameObject[] _powerups;
- Adding [] is how you make a variable an array. - Save, then select the Spawn_Manager.
- Head to the inspector and find the script component.
- There, hit the drop down arrow beside Powerups.
- Where it says Size, put 3.
- After that, it will say Element 0, 1 ,2.
- Drag Triple_Shot_Powerup in element 0.
- Speed_Boost_Powerup in 1.
- Shield_Powerup in 2.
- Head back to the SpawnManager script and find the method, SpawnPowerupRoutine().
13. Add the above code.
- By making the variable “randomPowerup”, we can spawn a random element from 0–2 in our new _powerups variable. They will still spawn normally and be randomized.
- That int variable will only use 0, 1 , and 2. It will only call numbers between 0 and 3, not including 3.
Be sure to test this out and wreck some enemies!
This has been how to set up a Shield system! I think we are done with powerups for a while. Next, let’s make a User Interface!
Thank you for your time!