Creating our Second Powerup — Speed Boost, and using our first Switch statement!
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!
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:
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!
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!
Now we’ll create the Logic for when an Object holding a Powerup script collides with the Player:
The if/else logic:
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!
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:
And then utilize these variables by creating a Coroutine with our Speed Boost logic, and a public method to call our Coroutine:
- 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:
And voila! Our Player ship can now evade the Enemy ships with ease! (or run into them faster!)
Next up we’ll be creating Spawning logic for our Powerups so they can rain from the sky(?) as well!