Thrusters Scotty! I need more Thrusters!

Objective: activate the Player’s thrusters upon a specific key command, and return to normal speed once the same key is released.

For the next feature, I was asked to give the Player an increase in speed (thrusters) when pressing down the Left-Shift key on my keyboard. I developed this in the following steps:

→ Detect the Left-Shift key input;

→ Change the Scale of the thrusters to give the Player a visual cue that the thrusters have been activated. Return the Scale back to normal upon release of the Left-Shift key.

→ Increase the Player’s speed to 10.0f when Left-Shift is detected. Return the speed to its previous value (default value or value set by the Speed PowerUp).

The first step was simple: use (Input.GetKey(KeyCode.LeftShift)) as a condition to change the transform’s scale. I first tried to implement this into the PlayerScript’s Update() method, but quickly ran into trouble controlling the scale of the thrusters only, and not the entire Player game object. I was having trouble figuring out how to only control the child game objects.

I solved this by simply creating the ThrustersScale script and attaching it to the two children (left and right thrusters). For the time being, I hard-coded their default values, as well as their increase in scale whenever a Left-Shift key is detected.

Holding the Left-Shift key down sets the condition to change the scale of the thrusters.
Notice how the scale of the children (thrusters) changes as the Left-Shift key is depressed.

I then returned to my PlayerScript and added the following two IF statements to the Update() method.

The first one, like the ThrustersScale script, looks for Left-Shift key input, and when held down executes the PlayerThrustersActivate() method.

The second one looks for the Left-Shift key to be released, upon which it executes the PlayerThrustersDeactivate() method.

Setting the conditions to either activate or deactivate the Player’s thrusters.

PlayerThrustersActivate() simply hard-codes the _speed value to 10.0f. Why? Well, this is what I felt was a reasonable speed, since anything much higher made the movements of the Player harder to control.

PlayerThrustersDeactivate() was a bit more tricky since I needed to take into account whether or not the Player had collected a Speed PowerUp. That particular PowerUp, in my rendition, multiplies the default speed (5.0f) by a factor of 1.75f which equates to 8.75f. The Speed PowerUp has a set lifespan of 5.0f seconds, and I used the _isPlayerSpeedBoostActive bool to verify its state. If TRUE, it uses the default _speed value of 5.0f and multiplies it by its _speedMultiplier (1.75f). I preferred not to hard-code this value (8.75f) for now in case I wish to change the _speedMultiplier default value later in my game project. I suppose I should eventually need to revisit how I set and store my default _speed value as well to avoid potential issues in the future. What I mean by this is that if I were to change my _speedMultiplier to a value greater than 2.0f, then pressing the Left-Shift would decrease the Player’s max speed since I have it hard-coded at 10.0f! I guess it all comes down to how you wish to implement features in your game; after all, you may decide it’s logical in your rendition to have the Speed PowerUp give your Player a higher velocity than when its thrusters are active.

PlayerThrustersActivate() and PlayerThrustersDeactivate() methods.

So there you have it. By holding down the Left-Shift key, your Player’s speed has by default doubled momentarily. The most appealing visual cue, other than the apparent increase in movement speed, is the scaling of the Player’s thrusters to indicate a boost in velocity. I hope you will find this article useful in your own Unity game projects. Thanks for reading :)

Military member with 33+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.