Unity New Input System Tutorial

This post is over a year old, the information may no longer be up to date.

I recently started getting into Game Development again, starting and then cancelling my first planned 2D Game The Lonely Helmet and moving onto a new one after what I learned. During making The Lonely Helmet, I learned that by default Unity ships with their ‘old’ Input System (“OIS”) still. This isn’t a bad thing per-say, it gets the job done if you’re wanting to make a game for keyboard and mouse, but when you start needing to consider things Controllers or Touch Screen, it’s a steaming bag of hot garbage.

That’s where the ‘new’ Input Sytem (“NIS”) comes in, Unity released this (version 1.0) in Unity 2019.1, so it’s been out for quite some time so I do find it a bit insane that’s not in my default, but I digress.

This is more for me to look back on in 6 months when I ultimately forget how the hell I set it up, but if it helps someone else too, that’s always a good thing!

# Setup

NIS requires Unity 2019.1 or higher and .NET 4 Runtime, .NET 3.5 Runtime isn’t supported.

You need to install the NIS since it’s not included by default, you can do this by opening Unity Package Manager (Window > Package Manager), ensure that All Packages is selected on the top left and then search for Input System and install. At the time of writing, 1.0.2 is the most recent available version.

Once installed, you should get a warning pop up alerting you that the NIS has been installed but not enabled. At the moment your Project is still using UnityEngine.Input, click Yes and Unity will restart and disable the OIS and enable the NIS.

# Input Methods

There is two ways you can use the NIS, getting input directly from an Input Device and through Input Actions. I’ll be covering the Input Action method as it, in my opinion, the better of the two, however an example of the Input Device method is below if you’re interested.

1
2
3
4
5
6
7
8
void PlayerMovement() 
{
    var controller = Gamepad.current;
  
    Vector2 move = controller.rightStick.ReadValue();
  
    // Continue Movement Code...
}

As you can hopefully tell from this though, it’s not ideal as you’d need to do the Input Device method for each type of Device, but it’s worth knowing that it exists.

# Character Controller

It’s worth noting that this is not a complete Character Controller, however it can be used as the base for one. You could also convert a Character Controller that uses the OIS to the NIS using this. I am assuming that you already have a Player object with Colliders and a Ridgidbody.

# PlayerInput Component

On your Player object, click on Add Component in the GameObject inspector and add a Player Input component to your object.

Once you have your Player Input component on your object, you should see a warning notice stating that “There are no input actions associated with this input component yet.”, under that there is a Create Actions… button which you’ll want to click.

When you click the button, Unity should ask you where you want to save the Input Actions asset to, choose a place that works for you. Once saved, the .inputactions asset will now be aligned to your Player Input component.

It should bring up an Editor Window where you can edit the default set to fit your project needs, however it should be ideal for a starter the way it’s already configured.

# PlayerInput Actions (Part 1)

Now that the Player Input component has it’s actions (if it doesn’t, drag the input actions asset into Actions on the Player input component in the Inspector), change the Behaviour option to Invoke Unity Events.

If you’re using the default Input Actions set, you should see Player and UI under the Events section of the Player Input component. These will be used later.

# Player Movement Script

On your Player object, click on Add Component in the GameObject inspector and add a new script called PlayerMovement to your object, once the script opens in your preferred IDE, paste the below script in to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour 
{
    public float movementSpeed = 25f;
    public Rigidbody2D rb;
    public float jumpForce = 10f;
    private float movementX; // left/right
  
    private void FixedUpdate() 
    {
    	rb.velocity = new Vector2(movementX * movementSpeed * Time.deltaTime, rb.velocity.y);
    }
  
    public void Move(InputAction.CallbackContext ctx)
    {
    	movementX = ctx.ReadValue<Vector2>().x;
    }
}

This is a very simple script, but it’s hopefully enough to give you an idea of what we’re actually doing, and what you can do.

# PlayerInput Actions (Part 2)

Now that you have your PlayerMovement script, go back to the Player Input component on your Player object to the Events section. Under Player you’ll have an item called Move (CallbackContext), drag the script Component under your Player Object into the Move context. Click on the dropdown, select PlayerMovement and select your Move method.

Now if you run your game, you should be able to control your player with A and D or the analog sticks on a Gamepad.

# Conclusion

In conclusion, you should now have a very simple, but working, Character Movement script. There’s a lot more that you can do with this and it’s definitely worth learning it over the OIS, Brackeys on YouTube is a good starting place now that you have the basics down.

//

Song Addiction at he moment: WASTEDJU - “e-girl”

Catch. 😊x