Adding Features: Thrusters

Austin Hall
3 min readOct 28, 2022

--

Right now, in our space shooter game, we have a speed boost powerup. That powerup lasts for five seconds and then we go back to normal speed.

If there is one function that I believe every game should have, it’s the sprint function. or in this case the speed boost. If I am moving, I want to be able to sprint on command.

For this feature we will call it thrust (you know… because were flying).

Often times, the simplest solution is the correct one, so let’s start simple.

Let’s talk about the logic. We are going to use the left shift key. we want our speed boost active while we are holding down the key.

We could use something like the code snippet below.

However, when we run the game, we will see that the speed variable continues to rise with every frame that passes. This means that we are multiplying our speed by 2 60 times per sec. NOT what we want.

Unity has other functions that read user input. Two of those are GetKeyDown, and GetKeyUp. As you may have figured out, GetKeyDown reads when the key is pressed down and GetKeyUp reads when the key is released.

Using those functions we can say that if the left shift key is pressed we increase our speed. if the left shift key is released, we return to normal speed.

First, I created a thrust variable. I used the Serialize Field attribute so that I can adjust it in the inspector.

Next, I used the GetKeyDown function to add my thrust speed to my speed. Then I used the GetKeyUp function to subtract my thrust speed from my speed. I could have multiplied/ divided, but I chose to add/ subtract for this one.

Doing it this way allows me to increase my speed, even when speed boost is not in play. And, when I do pick up the speed boost powerup, I can still use the shift key to add thrust.

Here, you can visually see how the speed variable changes when I press and release the left shift key.

--

--

No responses yet