I hope you had a fantastic first week of the new year! Let’s make 2021 the best year ever for game development.

As with all my Feature Friday emails, I’ll be breaking down a specific feature of the Unity game engine. This week we’ll be going over a very important topic that relates to player interaction – input.

It is important to properly understand the different ways to use Input in Unity so you can most effectively take input from the player and translate that to actions in your game.

So today we’ll be going over:

  • How to Take in Input in Unity
  • Different Types of Input in Unity
    • Buttons
    • Axes
    • Keys

And as always, at the end of the Feature Friday article, I’ll be featuring something made with the Unity game engine. This week I am featuring a precision puzzle platforming game.

Before we go on, I should note that article discusses the standard Unity input system. Unity is in the process of creating a newer input system which we will explore in a future Feature Friday.

If you already feel comfortable with the standard Unity input system, have a look at some previous Feature Friday topics like Presets or Coroutines.



1. How to Take in Input in Unity

The benefit of the standard Unity input system is that there is virtually no setup to get you up and running.

When you create a new MonoBehaviour script in Unity, you already have everything you need to start taking in input from the player – no need to include any additional libraries or anything. This is because the Input class is part of the UnityEngine library.

You are able to take in input anywhere in any script that includes the UnityEngine library – but that doesn’t mean that you should. In a subsequent Feature Friday article, I’ll be talking about why you should create your own class to manage input so everything is in one central location, but don’t worry about that too much for now.

In general, you will most often be checking for player input in an Update() method but there are some cases where you would want to check for input in other functions throughout your code. One example of this is if you wanted to perform some different behavior in a function if the player was holding down a specific key.

The first most common type of input you might want to take in is a simple mouse click. To do this, setup your Update() method like this:

private void Update(){
  if(Input.GetMouseButton(0)){
    Debug.Log("Left Click");
  }
}

We’ll be breaking this down further in the next section but in general, every frame, this script would check if the mouse button at index 0 (left mouse button) is held down; if it is then it will print the message “Left Click” to the console.

Also notice how the Input statement is inside an “if” statement. The reason for this is that the Input statement returns a boolean of true if the mouse button is down or false if it is not. Different types of input will return different data types depending on the information relevant to you.

2. Different Types of Input in Unity

Buttons

The code snippet I showed you above works perfectly fine, but it is a bit misleading if you don’t know what you are looking out for.

The method I used – Input.GetMouseButton(0) – will return true every single frame the left mouse button is held down. Now this may or may not be the behavior you want to happen. 

If, for example, you wanted to fire a weapon one time every time the player clicks the left mouse button. If you tried to implement this with Input.GetMouseButton(0), the weapon would end up firing once every single frame the player was holding down the left mouse button.

Luckily there is a similar method called Input.GetMouseButtonDown() that will only return true on the first frame the player began holding down the specified mouse button.

Furthermore, there is another method, Input.GetMouseButtonUp() that returns true on the frame that the user releases the mouse button.

One use for this would be if you had a throwable weapon. The player could hold down the mouse button then an aiming UI would be displayed so they could aim their throw, then when the player releases the mouse button, the character in game would throw the weapon.

Moving beyond mouse buttons, we can define our own custom buttons that can return true on Up, Down, and Held Down checks.

Our custom buttons are defined and set in the Input manager in the Unity editor. Just go to Edit > Project Settings > Input Manager to open the input manager. Once you do that, expand the axes dropdown and you’ll see some predefined buttons such as “Fire1” or “Jump.”

You can increment the “Size” value to create a new button, give it a name and press a key you’d like to assign to that button.

If we wanted to create a reload button for the “R” key, we could set it up like this:

Now in our code, we would simply call:

private void Update(){
  if(Input.GetButtonDown("Reload")){
    Reload();
  }
}

So you’ll see that we can detect input on a button that we defined in the editor, simply by passing the name of the button in to the GetButtonDown() function.

Axes

Similar to custom buttons we can define our own custom axes. Axes are used when we want to track a value on an axis. This is represented on a scale from -1 to 1.

When looking at a standard analog stick on a game controller, there are two axes to track. The X axis tracks movement left to right and the Y axis tracks up and down.

With the stick in its neutral position with the X and Y axis return a value of 0. If the player pushes the stick all the way to the left, the X axis returns a value of -1 and Y axis returns 0.

Pressing the stick all the way up returns a value of -1 on the Y axis, however checking the box for “Inverted” will invert this value so -1 is down and +1 is up.

While uncommon, a number of people play games using an inverted Y axis, so it is a good idea to include an option in the settings menu of your game to invert the Y axis.

To get an axis value in our code, we would do something like this:

float _horizontalInput;

private void Update(){
  _horizontalInput  = Input.GetAxis("Horizontal");
}

Here we are setting the value of _horizontalInput to the current input value on the Horizontal axis. Note that “Horizontal” and “Vertical” are pre-defined axes that correspond to input on the left stick as well as mouse movement.

Keys

Instead of defining Buttons in the Unity Editor, we can use Keys as a quick way to access keyboard input in our game with no setup.

Again, if we wanted to press the “R” key to reload, we would do this in our code:

private void Update(){
  if(Input.GetKeyDown(KeyCode.R)){
    Reload();
  }
}

This function does have GetKey, GetKeyDown, and GetKeyUp variants that check to see if the key is held down, pressed this frame, or released this frame respectively.

This is a quick way to get input in your game – just be aware that it isn’t necessarily the best coding practice, because it isn’t always obvious which key does what, and you can get yourself into a bit of a mess if you want to change what key press belongs to a certain behavior.

Anyways, I do think that is enough information for now so I am going to break this article up into two parts. Look forward to the 2nd part next week where we will be going over topics like mobile input, multiplayer, and how to create your own input manager for best practice coding standards.


Featured Unity Game of the Week

Are you a fan of puzzle platformers? If so than this one is for you!

The game Beam by developer Binary Impact has you controlling a ball through various platforming challenges.

You use beams of light in order to help you reach new places you couldn’t before.

If the name Binary Impact sounds familiar, that is because this developer shares short tips about the Unity game engine to their Twitter account every Tuesday. You can check out the archive here: https://www.binaryimpact.de/unity-tips/

Also don’t forget to check out their game right here: https://beam-game.com/


I’ll chat to you next week to continue the conversation on input. I hope you are all staying well, and as always, keep on creating!

-Johnny Thompson
Turbo Makes Games

Leave a Reply

Your email address will not be published. Required fields are marked *