Keyboard Input in Unity

David Little
2 min readApr 3, 2021

Now that we can control the speed and direction of motion with the player object we next need to be able to direct that motion with user input. The source of that input will be the keyboard. Directions for right, left, up, and down will be defined as coming from specific keys on the keyboard.

First we will declare the variables that will control vertical and horizontal movement. That will look like this in the script…

These variables are then called with Input.GetAxis and labeled appropriately, like this…

..and are then used in the method with the appropriate axis as defined with a new Vector3.

And, the final script.

You can see the order where the variables are declared, then defined, and finally implemented. Now we can get the player input coupled with the speed variable to result in movement of the player object in the scene, at the will of the user.

--

--