Adding a thruster speed boost on keypress

Today, we are going to modify our existing boost behavior by adding a thruster boost the player can use without powerups at any time by holding down the Left Shift key.

The first thing I’ve done is modify my current “_playerBoostSpeed” variable by renaming it to “_playerBoostPowerupSpeed” throughought the player script.

Tip: After changing the variable name, right click the variable name and select “Quick actions and refactorings”.

This will give you an option to rename the variable throughout the entire script automatically!

I then added _playerBoostSpeed back in to represent the player boost from thrusters when the player holds down the Left Shift key.

We default the speed to 1 as this will be used as a multiplier.

CalcMovement() Method

We simply add the “ * _playerBoostSpeed” line into our Translate call.

Next we modify the current “_speedBoostActive” variable by renaming it to “_speedBoostPowerupActive”.

TIP: Another way to rename all references to this variable at the same time is to right click the variable before changing the name and clicking “Rename” and then “Apply” on the popup window once you’ve renamed it.

Once we’ve renamed our original variable, we add in the now new variable of “_speedBoostActive”.

I know, jumping through a lot of hoops here but this naming convention makes more sense to me and hopefully to any future coders reading it (including ourselves when we’ve long forgot what we were doing here).

We’ll change our method names as well for clarity.

Just to be safe, let’s verify the OnTriggerEnter2D() method in our Powerup script and make sure we’ve updated the SpeedBoostActive() call to SpeedBoostPowerupActive().

CheckForThrusterBoost() method

We add our CheckForThrusterBoost() method.

Two things to note here.

First, the Boolean variable is not technically necessary, but could be useful in the future.

Second, we use Input.GetKey instead of Input.GetKeyDown.

I always find this a bit confusing, because in my mind, if you want to know if the user is constantly holding a button down, you’d add the word down to the end…

Well, Unity disagrees, so just be aware of the differences here.

“Returns true while the user holds down the key identified by name.”

“Returns true during the frame the user starts pressing down the key identified by name.”

Last, we modify our Update() method to call CheckForThrusterBoost() at the beginning of each frame update.

That’s it, run it and now the player will boost whenever they hold down the left shift key.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Big Update in Space SIP Game Version 1.2

The best frameworks for cross-platform development

How to Develop Android App with Kotlin

Pandas Operations in Python — Series

Bash script to create directory if not exists

CS373 Spring 2021 Blog #10: Sriram Alagappan

Becoming a Developer (part 1)

If You Build It They Will come

Add Scenes

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
GameDev Dustin

GameDev Dustin

More from Medium

Adding a burst laser powerup

Using GameDevHQ’s FileBase Asset Database For HQ Models

Creating An fbx Emission Layer For Unity Using Photoshop & Blender

Day 107: Ladder System Part 1