Creating our Second Powerup — Speed Boost, and using our first Switch statement!

Frank Warman
4 min readApr 15, 2021

So we can shoot a bunch of Lasers, awesome. But what if we have really bad aim and don’t destroy our Enemy ships? What if we just need to ESCAPE the situation. Well… I think a handy Speed Boost Powerup would come in… handy!

Zoom Zoom! Gotta’ Go Fast!

Create The Powerup

First things first, we need to create some type of pickup that our Player can interact with to activate their MEGA SPEED!

We should be familiar with this if you have read the Triple Shot Powerup article:

Drag in our Speed Boost Pickup, rename, and scale to an appropriate size.

Adding the necessary components:

  • Box Collider 2D — Is Trigger is on. Resize to fit the sprite.
  • Rigidbody 2D — Gravity Scale to 0.
  • Powerup Script — We’ll be adding to this soon.
  • And remember to change the Sprite settings to sit in the FOREGROUND!
Speed Boost Components

Creating a Modular Powerup Script

We now have two GameObjects using our Powerup script, but there is no way to distinguish between which is which.

We’ll step into creating a Modular Script — a script that can be used for a variety of objects, with their own Logic.

To start we’ll create a [SerializeField] private int _powerupID, that we can set in the inspector to distinguish our Powerups through code!

_powerupID variable
Setting our Powerup IDs for each individual Powerup.

Now we’ll create the Logic for when an Object holding a Powerup script collides with the Player:

Pseudo Code. Important to write when implementing features so you don’t lose track of what you’re trying to achieve.

The if/else logic:

That’s a lot of code!

There is nothing wrong with the code above, it just looks like a lot. There is another way to achieve the same results, but looks a bit tidier. It’s up to you to choose which method to implement.

Introducing the Switch statement

In the case of using if/else logic to differentiate from the same variable (_powerupID), we can also use a Switch statement!

Switch statement compared to if/else logic.

Let me explain…

The first line denotes which method we’re using, followed by the variable we’ll be ‘switching’ the logic for: switch (_powerupID) {}

Then the code is broken up by the cases. _powerupID is an int value, so starting at 0, we write our code underneath the case for which we want to happen.

At the end of every case, we MUST use the break; command to signify the end of this case of code. The switch code will not work without this.

And lastly, the default case occurs when a number not specified is called. Like 5 for instance.

Creating Speed Boost Logic

Now we know how to differentiate between different powerups, let’s finish our Speed Boost!

In our Powerup script we’ll create some new variables:

Speed Boost variables

And then utilize these variables by creating a Coroutine with our Speed Boost logic, and a public method to call our Coroutine:

Method and Coroutine created.
  • speed *= _speedMultiplier — our current speed is multiplied by our _speedMultiplier, which is 2.
  • yield return new WaitForSeconds(_speedCooldown) — now the code waits for 5 seconds (our cooldown)
  • speed /= _speedMultiplier — our speed is now divided by our multipier (2), returning our speed ack to normal!

Activating Speed Boost

With logic completed, we can go back into our switch statement and call our new method:

Switch statement updated!

And voila! Our Player ship can now evade the Enemy ships with ease! (or run into them faster!)

Notice the speed value in the Inspector changes once the Speed Boost is collected!

Next up we’ll be creating Spawning logic for our Powerups so they can rain from the sky(?) as well!

--

--

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles