Adding the speed boost powerup

The first thing we are going to do is drag the sprite we’ve chosen for the speed powerup from our assets folder to our hierarchy view, rename it to “Speed_Powerup” and resize it to .5, .5, .5.

Let’s also go ahead and change the Y position of this new game object to 8 so that it is off screen.

Now, like we did with our Triple Shot Powerup, let’s add a Rigidbody2D component and a CircleCollider2D component.

Set is trigger to true and Gravity scale to 0.

Also add the Powerup script as a component to the game object.

Go ahead and create a prefab by dragging our new Speed_Powerup into the prefabs/powerups folder.

Let’s see some ID

Since the behavior for all of our powerups will be very similar, essentially spawn randomly on top of screen and move down, we can use the same Powerup script for each of them.

We’ll create an ID system to track which result we want to activate based on which Powerup is in use.

To do this, we simply create an integer variable called powerupID.
We’ll set this to 0 for the Triple Shot, 1 for the Speed boost, and 2 for the Shield powerup.

Back in the Unity editor, we’ll select our powerup prefabs and make sure the “PowerupID” variable is set to match each powerup as appropriate.

Switch it up

We’ll use a switch statement to differentiate between the different powerups and act accordingly.

“Use the switch statement to select one of many code blocks to be executed.”

“When C# reaches a break keyword, it breaks out of the switch block.”

“The default keyword is optional and specifies some code to run if there is no case match”

We’ve modified our Powerup script, OnTriggerEnter2D() method by nesting a switch statement in the if statement.

Our code will first null check the player script on the “other” object, and if found, will run this switch statement.

The value of _powerupID will determine which “case” section of code will run.

The “break” keyword will ensure that the switch statement is exited immediately once the case statement is complete.

We never want to do more processing work than necessary with our code.

We use the “default” keyword to catch any unexpected values in the _powerupID variable.

Ideally, this code will never run, but if it does, we’ll know we need to fix something thanks to the debug log.

Make it happen

So our switch statement looks pretty and is easy to read, but it doesn’t work.

Yet.

We have the associated “TripleShotActive()” method on our player script, but we don’t have any code related to speed or shield powerups.

Add speed powerup code to Player script

First, we need to add some variables to our Player script.

We want to add a variable, with SerializeField for game designers, to track or modify what speed the player will travel at when the speed boost is active.

We also need a Boolean variable to track that active status of the speed boost.

Again, since the behavior is very similar between powerups, we can use our TripleShot powerup code as a guide.

We add the 2 methods shown above to activate the speed boost and to turn off the speed boost after a preset time period.

The main different from the triple shot methods is we also need to modify and reset our _playerBoostSpeed variable as shown above.

Modify CalcMovement() method

We could implement our speed boost code in a variety of ways. To avoid using an if statement in the CalcMovement() method, I’ve decided to use the _playerBoostSpeed variable as a multiplier rather than the actual all-in value of the player speed.

This way, we simply add a “ * _playerBoostSpeed ” to our transform.Translate and voila, no further changes to our movement code are necessary.

Since the default _playerBoostSpeed = 1, this will not change our player speed initially.

We do so by modifying it in our SpeedBoostActive() method.

Finding ways to write our code in a more elegant manner is always a good idea and it feels pretty good too.

We’re not done yet though.

We still need to activate this new player code in our Powerup script.

One simple statement added to our switch code and we are good to go.

Spawn Manager

The last bit of code we need to implement is in our spawn manager. Right now, if we place our Speed_Powerup prefab in the hierarchy we can test our code, and it works, but we want these to spawn more spontaneously.

Up until now, we have had just the one powerup and our method was named accordingly as “SpawnPowerupRoutine”.

Since we want to spawn these powerups at different times from each other to add variety to our game, we are going to rename this to “SpawnTripleShotRoutine” and create a new method for the speed powerup.

Not at the beginning…

It’s been driving me nuts that enemies and powerups all initially spawn as the game starts.

For now at least, I’m ok with the enemies spawning as soon as I run the game.

However, I want my powerups to spawn at a random start interval.

This is easy enough to fix. We simply move the yield statement from the end of these methods to the beginning.

As you can see above, we’ve added our SpawnSpeedBoostRoutine method as well to the Spawn Manager script.

We need to add a new variable to store our Powerup_Speed prefab for spawning purposes.

We update our Start() method a separate StartCoroutine line and I’ve varied the spawn interval variables that will be passed from the TripleShot spawn interval variables.

I want my speed boost to appear less frequently, but still randomly.

In Unity, we assign our Speed_Powerup prefab game object to the Spawn Manager.

If we run the game now, we find that the spawning for powerups has changed according to our design.

Animate it!

Next we animate our Speed_Powerup game object as shown above in the same manner we did with the Triple_Shot_Powerup game object.

Don’t forget to apply the overrides to your prefab!

Once you have applied the overrides, you can safely delete the Speed_Powerup game object from the hierarchy window.

Test it out

Go ahead and hit play, have some fun and enjoy the progress you’ve made!

In the next article, we’ll be adding the shields powerup.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store