Project 1: Space Shooter
Dodging the danger with thrusters
How to use GetKey in Unity
The next feature to be implemented is thrusters to give a small speed boost for the player when they keep pressing the left shift down.
For this to work Unity gives a method Input.GetKey, which looks for any key that has been pressed down and kept there. To make it react only to the left shift the KeyCode.LeftShift event is used as an argument for the GetKey method.
Then all there is to it is to add an if-statement to my player script’s Movement method to activate the thruster boost when the key is held down or to revert back to the normal movement if it is not held down.
I did add visuals for it as well. Once the thruster boost is used the player’s thrusters change color to indicate the boost being active and vice versa when they are not the color reverts back to the original color. This was achieved by saving the color as the sprite renderer’s color.
Since I have two thrusters I went through them in a foreach-loop. This loop is designed to go through every element in an array, and for this reason I chose to use it.