2D Galaxy Shooter — Speed Boost Powerup using “Switch” statements
Hello all!
Today, I will be making a “Speed Boost” powerup for my game. When collected, it will multiply the player’s _speed variable by 2.
Adding and Animating the Sprite
- Find a good .PNG sprite that represents a speed boost, add it to the hierarchy, and rename it “Speed_Boost_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
- 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, if you do, continue.
- Go to the Animation window, create an animation file named “Speed_Boost_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.
Add the Speed Boost Behavior to the Powerup Script
Here, we will change the Triple Shot, Speed Boost, and upcoming Shield powerups to be called by a “Switch” statement. Switch statements look much cleaner than a bunch of “if else” statements. You’ll see why soon.
Add the Variable above, then head to the script component for Triple Shot and make it 0, then make Speed Boost, 2.
Before, we had an if statement that would only call the ActivateTripleShot() method in the Player’s script. Now, looking above, if the powerup collides with the player, then if the powerup’s ID = 0, then Triple Shot is active and if 1, then Speed Boost is active.
- We will get into the Shield powerup in my next article.
Switch statements use “cases” to determine what it will do and a “break” to end.
It’s similar to an if statement where “if (_powerupID = 0)”, then Triple Shot is active, and you could go further and say “if else (_powerupID = 1)”, then Speed Boost is active. Say you have 50 powerups, would you want to make 50 if else statements or just one, cleaner looking switch statement?
Player Behavior with Speed Boost
If the player collects the Speed Boost powerup, then multiply the speed by 2 for 5 seconds.
- Head to the Player script.
- Add variable — [SerializeField] private bool _isSpeedBoostActive = false;
- In the CalculateMovement method, find where the movement is.
- Add the existing movement code into the “else” statement, then match above.
- This is saying that when Speed Boost is true, speed will be 10, and when false, go back down to 5. - Add 2 new methods.
Looking back at the new switch statement in the Powerups script, it will call the ActivateSpeedBoostPowerup() method, starting the coroutine, then ending the powerup after 5 seconds.
This has been how to implement a Speed Boost powerup! Next, I will go over how to make a shield to protect the player from one hit and make powerups spawn in the spawn manager using an “array”!
Thank you for your time!