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.
MOST SEARCHED VIDEOS FROM MY CHANNEL
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.
En la jerarquía hay un GameObject llamado #1 Input. Este objeto tiene todos los elementos relacionados a la estación de entradas.
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.
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".
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?
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.
In this way, we can ensure that the Input station fulfils its function.
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.