Thrust!
Objective: Increase the player’s speed on key press in Unity
Unity’s built-in Input.GetKeyDown method detects when a key has been pressed. Unity’s built-in Input.GetKeyUp method detects when a key has been released. You want to increase the Player’s speed when the left shift key is pressed and reset the Player’s speed back to normal when the left shift key has been released.
Get KW Development LLC’s stories in your inbox
Join Medium for free to get updates from this writer.
Initialize a new private field in the Player script and call it _thrustMultiplier, set its value to 2.75f. Initialize another private field in the Player script and call it _thrustActive and set its value to false. Create a new method in the Player script called IncreaseSpeed. Inside the IncreaseSpeed method, check for the pressing of the left shift key by using this code: if (Input.GetKeyDown(KeyCode.LeftShift)). Inside that function, multiply the speed by the _thrustMultiplier and set _thrustActive to true.
To cool it down, make a Coroutine method and include a wait time of one second before resetting and then divide the _thrustMultiplier by the speed and set _thrustActive to false. Call the Coroutine method inside the if (Input.GetKeyUp(KeyCode.LeftShift)) function.
Finally, put the IncreaseSpeed method inside the Update method.









