#1 Read Input in Unity. Input Class

By GameDevTraum

Introduction

In this article we are going to see several methods from the Input class that will alloud us to read keyboard and mouse inputs in Unity.

Go to the Project’s main page

Before we begin, I invite you to watch the following video that resolves this problem, make sure to activate the english subtitles.

Procedure

In the first video we are going to work on the input station, which consists of models for the “WASD” keys and mouse buttons.

The goal is to get them to change color when the corresponding entry is pressed.

project unity3d 3d model made in blender of wasd keys, a mouse and a sign that says input station
Fig. 1: GameDevLab input station.

En la jerarquía hay un GameObject llamado #1 Input. Este objeto tiene todos los elementos relacionados a la estación de entradas.

Fig. 2: Hierarchy. The input station is the GameObject called #1 Input.

This GameObject is assigned the Read Input script, which will be responsible for reading the entries and performing actions on the elements of the station.

Fig. 3: Script Read Input assigned to GameObject #1 Input.

There’s no need to do anything in the hierarchy. Go to the folder “Scripts to Complete”, “Video 1 – Read Inputs” and open the script “ReadInput”.

example c# script for unity 3d
Fig. 4: Script Read Input not completed. The missing code goes into the Update method.

Why should the code go in the Update method?

All scripts that extend their MonoBehaviour class behavior have an implicit Update method, which is automatically evaluated in each frame of our game.

As explained in the video, if our game runs at constant 60 fps, the Update method runs 60 times per second.

The reading of the Inputs is a random event, so we cannot determine when the player will press a button. For this reason, when we need to read the entries, we must do so in all frames.

How do we read the inputs?

Let us imagine that we can ask the computer in colloquial language about the state of the entries, what questions would we ask?

Fig. 5: Questions to ask.

If the answer to these questions is yes, we will execute an appropriate action for each one.

We already have the idea, now we just have to ask these questions using a language that the computer understands.

Using the if sentence, we can ask questions whose answer is true or false.

Unity’s Input class handles the inputs, so we use their methods to solve the problem. We have the GetKey method for the keys and GetMouseButton for the mouse buttons, in the argument of these methods we indicate the key using the enum KeyCode and the mouse button using integers, as can be seen in figure 6.

Solution

Fig. 6: The missing code is complete.

In this way, we can ensure that the Input station fulfils its function.

project unity3d 3d model made in blender of wasd keys, a mouse and a sign that says input station. the key w is in green color
Fig. 7: Input station. W key pressed.

project unity3d 3d model made in blender of wasd keys, a mouse and a sign that says input station. the key a is in green color
Fig. 8: Input station. Key A pressed.

project unity3d 3d model made in blender of wasd keys, a mouse and a sign that says input station, left clic is in green color
Fig. 9: Input station. Click left.

project unity3d 3d model made in blender of wasd keys, a mouse and a sign that says input station, right clic is in green color
Fig. 10: Input station. Right click.

Conclusion

As a first conclusion, I would like to mention how we went from thinking about the problem in colloquial language (figure 5) to writing it in C# language (figure 6). Thinking about the problems in this way is simpler, because we abstract ourselves from the programming language.
An intermediate step between this colloquial language and the C# code would be the pseudocode.

Reading Inputs is the way we know what the player wants to do, so we have to define what tickets we are going to offer and when we want to read them (for example perhaps we are not interested in reading the WASD tickets when we show a cinematic).

Input events are random, that’s why we must read them continuously using the Update method.

To avoid complications it is advisable to read the inputs in a single script.

There may be other types of inputs, such as a JoyStick or tactile entry. We must study Unity’s Input class to understand how to handle them.