Unity's “Input System” is used to define user controls and manage how input can be mapped to buttons on the controller device. The new Input System offers a pack of extra features that allow the game developer to be flexible with creating controls for their games, such as, defining action-based input and allowing rebinding controls.
The older version was called the “Input Manager” and was built into Unity. The newer version, called the “Input System,” needs to be imported from the Package Manager.
Input.GetButtonDown
methodThis method returns true
, when the instance of the button is pressed. By instance, we mean the exact "frame". When the Update()
method is called again, the state resets, and the method keeps returning false
until the button is pressed again.
public class ExampleClass : MonoBehaviour{public GameObject projectile;void Update(){if (Input.GetButtonDown("space"))Debug.Log("The spacebar was pressed!!!");}}
In the code above, space
is a key code predefined in the input manager for the spacebar key on the keyboard. The Input.GetKeyDown()
method will read the input from the keyboard only once, i.e, in the frame that the key was pressed and not the subsequent frames that the key was pressed. However, this method was slightly altered in the new input system.
Note: We can make use of both new and old input systems in our project but we need to make sure that one script only makes use of one type at a time.
Let’s look at how we can achieve a similar functionality using the new input system. We’ll go through it step by step. First, we need to create a new “Input Actions” asset by clicking on “Assets -> "Create" -> “Input Actions.” Then, we’ll see the following window.
In the window that opens, we need to:
Add a new “Action Map” by clicking on the plus icon, as shown in the first slide.
Then, in the “Actions” column, we need to add a binding to the action that is already present by clicking on the dropdown menu and selecting the binding.
Next, up we need to bind a key to this action and we can either manually select the path to the key from the drop down menu or even make Unity listen for any keystrokes.
Finally, we can close the window. Make sure to save the settings when closing the window.
Now, to check out this action in action (no pun intended), we need to make use of the “Player Input” component. So, we’ll create an empty game object and attach the component to it. We can create an empty game object by clicking on “GameObject” in the menu bar and then selecting “Create Empty.”
Now, we’ll create a script that is able to read the actions attached to the empty game object and perform operations accordingly. We'll attach this script on the object as well.
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.InputSystem;public class PlayerController : MonoBehaviour{[SerializeField]private InputActionReference press;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){press.action.started += context => Debug.Log($"{context.action} started");}}
In the script above:
Line 11: Here, we’re defining the reference for the “Input Action” we want to use.
Line 22: In the Update()
method, we're using the started
method to check if the button was pressed or not. This is the equivalent of Input.GetButtonDown()
method. It will only trigger during the frame the button is pressed. Furthermore, we're printing to the console when the button is pressed.
Finally, we need to specify the InputActionReference
in the inspector window. Here, we need to specify the action that we're referring to. We’ll add the action that we created earlier by simply clicking on the drop down menu and selecting the action present in our assets, as shown in the slides below.
As a result, when we press the play button and press the spacebar, we'll see a message pop up in the console. You can test out a live demo below. This demo will change the color of the text being displayed on the screen every time the spacebar is pressed.
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
This demo demonstrates that the input from the controller will only be read once per frame, and it won’t matter if the user has the key constantly pressed or not—the input will only be read once.
Free Resources