#1 Read Input in Unity. Input Class

Introduction

In this arti­cle we are going to see sev­er­al meth­ods from the Input class that will alloud us to read key­board and mouse inputs in Unity.

Go to the Project's main page

Before we begin, I invite you to watch the fol­low­ing video that resolves this prob­lem, make sure to acti­vate the eng­lish sub­ti­tles.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Procedure

In the first video we are going to work on the input sta­tion, which con­sists of mod­els for the "WASD" keys and mouse buttons.

The goal is to get them to change col­or when the cor­re­spond­ing entry is pressed.

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

En la jer­ar­quía hay un GameOb­ject lla­ma­do #1 Input. Este obje­to tiene todos los ele­men­tos rela­ciona­dos a la estación de entradas.

Fig. 2: Hier­ar­chy. The input sta­tion is the GameOb­ject called #1 Input.

This GameOb­ject is assigned the Read Input script, which will be respon­si­ble for read­ing the entries and per­form­ing actions on the ele­ments of the station.

Fig. 3: Script Read Input assigned to GameOb­ject #1 Input. 

There's no need to do any­thing in the hier­ar­chy. Go to the fold­er "Scripts to Com­plete", "Video 1 — Read Inputs" and open the script "Read­In­put".

example c# script for unity 3d
Fig. 4: Script Read Input not com­plet­ed. The miss­ing code goes into the Update method.

Why should the code go in the Update method?

All scripts that extend their MonoBe­hav­iour class behav­ior have an implic­it Update method, which is auto­mat­i­cal­ly eval­u­at­ed in each frame of our game.

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

The read­ing of the Inputs is a ran­dom event, so we can­not deter­mine when the play­er will press a but­ton. For this rea­son, when we need to read the entries, we must do so in all frames.

How do we read the inputs?

Let us imag­ine that we can ask the com­put­er in col­lo­qui­al lan­guage about the state of the entries, what ques­tions would we ask?

Fig. 5: Ques­tions to ask.

If the answer to these ques­tions is yes, we will exe­cute an appro­pri­ate action for each one.

We already have the idea, now we just have to ask these ques­tions using a lan­guage that the com­put­er understands.

Using the if sen­tence, we can ask ques­tions whose answer is true or false. 

Unity's Input class han­dles the inputs, so we use their meth­ods to solve the prob­lem. We have the GetKey method for the keys and Get­Mouse­But­ton for the mouse but­tons, in the argu­ment of these meth­ods we indi­cate the key using the enum Key­Code and the mouse but­ton using inte­gers, as can be seen in fig­ure 6.

Solution

Fig. 6: The miss­ing code is complete.

In this way, we can ensure that the Input sta­tion ful­fils 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 sta­tion. 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 sta­tion. 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 sta­tion. 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 sta­tion. Right click. 

Conclusion

As a first con­clu­sion, I would like to men­tion how we went from think­ing about the prob­lem in col­lo­qui­al lan­guage (fig­ure 5) to writ­ing it in C# lan­guage (fig­ure 6). Think­ing about the prob­lems in this way is sim­pler, because we abstract our­selves from the pro­gram­ming language. 
An inter­me­di­ate step between this col­lo­qui­al lan­guage and the C# code would be the pseudocode.

Read­ing Inputs is the way we know what the play­er wants to do, so we have to define what tick­ets we are going to offer and when we want to read them (for exam­ple per­haps we are not inter­est­ed in read­ing the WASD tick­ets when we show a cinematic).

Input events are ran­dom, that's why we must read them con­tin­u­ous­ly using the Update method.

To avoid com­pli­ca­tions it is advis­able to read the inputs in a sin­gle script.

There may be oth­er types of inputs, such as a Joy­Stick or tac­tile entry. We must study Unity's Input class to under­stand how to han­dle them.

Scroll to Top
Secured By miniOrange