Keyboard input in Unity

3D Game Design with Unity

🕑 This lesson will take about 7 minutes

In Unity, you can set up your game to detect specific keypresses and then react to those keypresses. However, there is a better way of setting up input using Unity’s built-in Input Manager.

The video below will show how to use the Input Manager and how to also detect specific keypresses in your game. After you’ve finished watching the video, scroll down to see the sample code.

Is YouTube blocked at school? Watch on Google Drive.

Input Manager

Setting up your game to detect specific keypresses doesn’t allow players to remap the game input controls to suit their preferences. However, if you use the Input Manager you can allow the player to map the controls to their own liking or use the defaults (eg. the W, A, S and D keys).

You can set up controls for each of the different axes in the Input Manager by clicking Edit > Project Settings > Input. Then you can write a script using the GetAxis() method to detect key presses.

Below is some sample code that will display horizontal and vertical movement in the console when the W, A, S and D keys (or arrow keys) are pressed. Simply place this code inside the Update() method in a script that is attached to an object inside your game’s scene (eg. the Main Camera object).

Save the script and then run the scene. Check the output in the console after pressing the W, A, S and D keys and the arrow keys.

Try out the code above and see what happens when you press the W, A, S and D keys or arrows keys on your keyboard. Also, did you notice how the sentences and values are combined in the print statements above? This is an example of concatenation (where strings are joined together).

Specific key input

Although it is generally a good idea to use the Input Manager for key input detection, it may sometimes be necessary to check whether a specific key has been pressed by the user. To do this, you can use the GetKey() method. This method will read a keycode which corresponds to a specific key on the keyboard. It will then return either a true or false value depending on whether the key is pressed or not.

As an example, to check if the A key is being pressed, you can use the following code. Just place this code inside the Update() method in a script that is attached to an object inside your game’s scene (eg. the Main Camera object).

    bool isKeyDown = Input.GetKey(KeyCode.A);

Tip: In MonoDevelop, if you are not sure what keycode corresponds to a key, you can begin typing in the name of the KeyCode object followed by a period. A popup menu should appear, showing you all of the possible choices. This automatic popup feature in MonoDevelop can be used for many other objects/methods too and is pretty handy.

You can use a simple if statement to specify an action to occur when a keypress is detected. For example, the following code will output a message to the console saying that the ‘A’ key has been pressed when it is detected that the ‘A’ key has been pressed.

if(Input.GetKey(KeyCode.A)) {
print("You pressed the 'A' key.");
}

Save the script and then run the scene. Check the output in the console after pressing the ‘A’ key.