2D Galaxy Shooter — Speed Boost Powerup using “Switch” statements

Marcus Cohee
3 min readAug 17, 2021

--

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

  1. Find a good .PNG sprite that represents a speed boost, add it to the hierarchy, and rename it “Speed_Boost_Powerup”.
  2. Add a Box Collider component, check “Is Trigger”, and make the hitbox.
  3. Add a Rigid Body component and change the “gravity scale” from 1 to 0.
  4. Add the “Powerups” script
  5. Make it a prefab by dragging it into the prefabs folder.
  6. If you do not have the assets to animate the sprite, go to the next section, if you do, continue.
  7. Go to the Animation window, create an animation file named “Speed_Boost_Powerup_anim.anim” in the Animation folder.
  8. Hit the record button on the top left of the animation window.
  9. Now drag the pictures into the DopeSheet and stop recording.
  10. Hit the play button to test it out.
  11. 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.

_powerupID can be changed in the script component for each powerup

Add the Variable above, then head to the script component for Triple Shot and make it 0, then make Speed Boost, 2.

Switch statement for the three powerups in our game

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.

  1. Head to the Player script.
  2. Add variable — [SerializeField] private bool _isSpeedBoostActive = false;
  3. In the CalculateMovement method, find where the movement is.
  4. 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.
  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!

--

--

Marcus Cohee
Marcus Cohee

Written by Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!

No responses yet