Nerd For Tech
Published in

Nerd For Tech

Unity Guide

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.

Player sprite

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:

Player 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.

Movement input

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:

In this window we’ll be able to determine the actions to execute according to what inputs we bind to them.

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:

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.

We’ll use this class to be able to use the actions from our input action asset.

For more information about how to create actions with the new input system, you can visit the Unity docs:

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:

  • Speed

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.

  • Rigidbody

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.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

open-Cv tutorial

The Beginner’s Guide to Your First Pair-Programming Experience

OpenStack , Simplified!

“Walrus” Operator Python 3.8.0

Announcing First 2021 DotVVM Meetup

hy I use Discord and why it has become so popular

⏰ WONO Presale - Renewal

Event | NESTFANS Open Culture Community Incentive Program

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

More from Medium

Cross platform input controller | Unity

Understanding Frustum and Occlusion Culling in Unity3D

Using Signal Emitters in Timeline to Access C# Scripts

My journey becoming a Unity game developer: Main Menu: Main Menu Music and Start button activated