New Unity Input System: Getting Started

In this Unity Input System tutorial, you’ll learn how to convert player input in your existing projects from the old Input Manager to the new Input System. By Ken Lee.

4.1 (18) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Creating an Input Action Asset

Once you’ve installed the Input System package, you’ll create an Input Action Asset to store the settings for your actions and bindings.

Open the Project window and select Settings from RW. Right-click, select Create ▸ Input Actions and rename it to MyControl.

Creating a new input asset

Setting up the Action Editor

Double-click MyControl in Settings to open the Action Editor, which helps you manipulate actions and control bindings.

Action Editor Window

Since this is a new window, take a look at the sections:

You can switch on Auto Save to prevent the problem, but it’s quite slow.

  1. Action Maps: Groups of actions that occur in the game. You can group actions for different purposes, like player, gameplay or UI.
  2. Actions: The list of actions and bindings associated with the selected Action Map. In this panel, you create, modify or delete actions and bindings.
  3. Properties: Edit the action or binding properties in this panel, such the type of action and the controls you associated with the binding.
  4. Save Assets: This is a very important function: You must click Save Asset after making any changes to the Input Action Asset. If you forget to save, the setting won’t work. Thus, you won’t see the expected result and may think there’s a bug in the code.

Now you’re ready to create your first action, the Jump action.

Creating a Jump Action

First, open the Action Editor and click the + icon in the ActionMap to create a new Action Map. Rename it from the default, New Action Map, to Player.

Then, in the Action panel, double-click New Action and rename it to a meaningful name: Jump.

Create Action Map

Finally, you need to add a binding to the Jump action. You’ll bind the Spacebar and Left Mouse Button to this action by following these steps:

  1. Select the Jump action, click the + icon and select Add Binding.
  2. Click the new binding item, <No binding>.
  3. Click the Path field in the Binding properties panel.
  4. Type Spacebar Keyboard and select Space [Keyboard] to create the binding for the Spacebar.
  5. Repeat steps 1–3 to create another binding for the Left Mouse Button.
  6. Type Left Button in the Path field and select Left Button [Mouse] to create the binding.

Congratulations, you’ve now associated the Jump action with the Spacebar on the keyboard and the left button on the mouse.

Jump Binding

Now to hook up those actions with your code!

Implementing the Jump Logic

First of all, you need to remove the old input logic from the project. Open Player.cs and navigate to the Update() method.

void Update()
{
    UpdateAnimation();

    if (!jumping && Input.GetKeyDown(KeyCode.Space))
    {
	HandleJump();
    }
}

As you can see, the current code triggers the animation updates, then it checks if the player has pressed the space bar in order to start a jump.

Now that the Jump action and its control bindings are ready, the next thing to do is link the action to the code.

Linking the Jump Action to the Code

Start by deleting the code in Update to remove the implementation of the old Input Manager so you can add Jump logic using the new Input System. Update will now only control the animations.

void Update()
{
    UpdateAnimation();
}

Save the script and go back to the editor. Select the Player object in the Hierarchy and add a PlayerInput component from the Inspector.

Next, you’ll drag MyControl to PlayerInput’s Actions. Make sure to set the Default Map to Player.

Adding

Finally, open Player.cs and add a new method called OnJump() with the following code:

public void OnJump()
{
    HandleJump();
}

You’ve associated this method with the Jump action by using this pattern to name it: public void On[Action Name Goes Here]().

For example, the Jump action invokes OnJump(), while the Attack action invokes OnAttack().

Click Save Asset in the Action Editor and run the game. Now you can use the SpaceBar or the left mouse button to make the player character jump. It’s really that easy!

New Jump Ready

Creating the Move Action

You’ve learned how to use the Input System to create a Jump action. Next up is the Move action! Move is similar to Jump, but it has a few key differences.

For example, the Jump action is a simple trigger event, while the Move action is an event that carries values: the movement direction, which comes from user input.

Again, you need to create the action and its binding. Start by going to Action Editor (double click MyControl if you lost the window) and click the + icon in the Actions panel to create a new action. Rename it to Move.

Next, open the Action properties panel, change Action Type to Value and Control Type to Vector 2.

Finally, remove <No Binding> by right-clicking and selecting Delete.

How to create a Move action

Now, you need to create the Move action’s bindings.

First, you’ll click the + icon in the header of the Move action. Then, select Add 2D Vector Composite, which will create four binding items corresponding to the up, down, left and right directions.

Now, you’ll set the path of each binding as follows:

  • Up: Up Arrow [Keyboard]
  • Down: Down Arrow [Keyboard]
  • Left: Left Arrow [Keyboard]
  • Right: Right Arrow [Keyboard]

Don’t forget to save the asset in the Action Editor!

Creating the Move binding

Implementing the Move Logic

Before adding new movement logic, you need to remove the implementation of the old Unity input.

Open Player.cs and go to FixedUpdate():

private void FixedUpdate()
{
    // 1
    float speedX = Input.GetAxisRaw("Horizontal");  // Left, Right
    float speedY = Input.GetAxisRaw("Vertical");  // Back, Forward
    moveVec = new Vector3(speedX, 0, speedY);

    if (jumping == false)
    {
        // 2
        UpdateWhenGrounded();
    }
    else
    {
        // 3
        UpdateWhenJumping();
    }
}

Note that FixedUpdate() is called in every fixed frame-rate frame.

Now, break this down:

These two values define the movement vector moveVec, which you use to control the direction of the player movement.

  1. Input.GetAxisRaw returns the value of Axis. Input.GetAxisRaw("Horizontal") gives the value of the X-Axis, while Input.GetAxisRaw("Vertical") gives the value of Y-Axis.
  2. The logic of the player character’s behavior while it’s on the ground.
  3. The logic of the player character’s behavior while it’s jumping.

Now, delete all the code prior to the if statement to remove the old input logic. Add the following code above the class definition:

using UnityEngine.InputSystem;

This allows you to access values from the new Input System.

Then, add OnMove(), which the Move action invokes.

public void OnMove(InputValue input)
{
    Vector2 inputVec = input.Get<Vector2>();

    moveVec = new Vector3(inputVec.x, 0, inputVec.y);
}

When a player presses the Up, Down, Left or Right keys, it passes a Move action to this method, along with the values. Here’s how the key presses affect the values:

  • Up: (0, 1)
  • Down: (0, -1)
  • Left: (-1, 0)
  • Right: (1, 0)
  • No Key: (0, 0)
  • Up and Left: (1, -1)

InputValue is a new type you may not know. This class has a Get\() method that you can use to access its values. In this instance, you want the 2D Vector Composite you set in the binding to calculate the movement vector.

Click Play to test the logic.

Move Ready