Moving with the new input system | Unity
A quick guide about how to implement movement using the new input system in Unity
Objective: Set up horizontal movement for our player using the new input system in Unity within a 2D game.
In the last post I covered how to introduce animated tiles for our game with Unity. Now, it’s time to begin setting up the player by implementing a simple horizontal movement mechanic using the new input system in Unity.
To start, let’s take a look at the sprite sheets that we have for the player animations:
Then, to get the initial sprite of our player, let’s slice the Idle sprite sheet by using the sprite editor:
Once sliced, let’s create a new empty gameobject to contain the initial player sprite so that it’s easier to manage all its components:
Next, let’s create a new script and attach it to the player gameobject:
Also, in order to handle the player’s movement and collisions with the environment, let’s attach a Rigidbody 2D and a Box Collider 2D. Once attached, we can edit the 2D collider to match the sprite:
Importing the new input system
And now, in order to be able to use the new input system from Unity, let’s:
- Open the package manager by clicking on Window > Package Manager.
- Select the Unity Registry packages at the top left.
- Find and install the Input System package by clicking on the Install button at the bottom.
- Wait until the package gets installed.
It’s probable that the next window will be prompted after the installation is completed. You can choose to disable the old input and enabling the new input system by clicking on Yes or just keep the same settings by clicking on No.
If you choose to leave the old input and you want to change the input handling you can:
- Click on Edit > Project Settings to open the project settings.
- Go to the Player tab.
- Scroll down to the Other Settings section.
- Change the Active Input Handling property to use the new input system or both old and new system.
Note: The Unity Editor will restart to apply changes to this settings.
Once that the Unity Editor recognizes the new input system, let’s begin by creating a new input action asset to handle the inputs from the player. You can create one by clicking on Create > Input Actions:
Then, to modify the input action asset, let’s double click on its file or click on Edit asset through the inspector to open the respective window:
Once the window is prompted, let’s create a new action map to handle the gameplay actions from the player by clicking on the next button:
An action map represents a collection of actions that can be enabled or disabled to use different inputs depending on what’s happening in the game. For example, we can have an action map enabled when the player is walking and another action map enabled when the player is driving a car.
Next, let’s modify the new action in our action map by:
- Indicating that it’s for movement.
- Changing the Action Type property to be of type value.
An action of type value will be monitored continuously (to be able to read it’s value when it changes) while an action of type button will be acting as a trigger once that a button gets pressed.
- Changing the Control Type to Vector2 in order to limit the input to be the X and Y axis values for our movement.
Then, in order to receive a Vector2 value from the inputs that will be bend to the movement action, let’s get rid of the default binding slot and create a new 2D Vector Composite:
Now, to indicate the input keys to receive the Vector2 values, let’s click on each direction and select the key that we want to map. Additionally, we can click on the Listen button and then click on the key to let Unity detect which key needs to be selected:
Finally, don’t forget to click on the Save Asset button to save the changes we did to the input action asset:
If you want to know more about the new Unity’s input system, you can visit the Unity docs:
Quick start guide
Note: For information on how to install the new Input System, see Installation. The quickest way to get started in…
Receiving values from the input action asset
And now, in order to receive the respective values from the movement action, let’s:
- Click on the input action asset.
- Enable the Generate C# Class property through the inspector.
- Click on the Apply button to create the C# class.
This will create a new C# class that will contain the input actions from the asset we modified. With this class, we’ll be able to keep track of the actions within MonoBehaviour components like our player’s script.
For more information about how to create actions with the new input system, you can visit the Unity docs:
Related pages: Input Actions are designed to separate the logical meaning of an input from the physical means of input…
Once the PlayerActions class is generated, let’s open the Player script to implement the movement. We’ll need the next global variables to do it:
This variable will indicate the speed of the movement of our player.
- Player actions
This variable will work as a reference to the new PlayerActions class where we’ll be able to receive the movement values from the inputs.
This variable will hold a reference to the Rigidbody2D of our player.
- Move input
This variable will store the current Vector2 value from the movement.
Then, in the Awake method, let’s initialize the respective references to the PlayerActions class and the Rigidbody2D component of the player:
Next, we need to manually enable the action map (which contains the movement action) that the player will be using initially by using the OnEnable method:
Also, let’s disable it by using the OnDisable method as a matter of good practice when working with actions:
Finally, let’s use the FixedUpdate method to implement the movement of our player by:
- Reading and storing the Vector2 value from the Movement action.
- Setting the Y axis value to 0 (temporal fix until we apply velocity on this axis).
- Setting the velocity of the Rigidbody2D to be the input value multiplied by the speed.
Testing the movement
Before running the game, let’s:
- Indicate a value to the speed variable of our player.
- Freeze the Z rotation of the RigidBody2D component (to avoid rotation while moving to the sides).
And now, if we run the game with Unity, we’ll see that the player moves as expected:
And that’s it, we used the new input system of Unity to implement the player’s movement! :D. I’ll see you in the next post, where I’ll be showing how to implement the player’s jump using the new input system and a Boxcast2D with Unity.