avatarMichael Quinn

Summary

The website content provides a comprehensive guide on how to handle input in Unity using the new Input System, contrasting it with the traditional Input Manager, and includes steps for installation, setup, and implementation with custom C# code.

Abstract

The article "Unity: Handling Input With the Input System" introduces the Unity Input System as a versatile and robust solution for managing input across various devices. It outlines a series of steps to integrate this system into a Unity project, starting with the installation of the Input System package via the Package Manager. The guide then proceeds to demonstrate the creation of an Input Action Asset and the addition of a Player Input component to a GameObject. It emphasizes the importance of setting the Player Input component's behavior to "Invoke C# Events" for optimal functionality. The article further details the process of writing a custom C# script, named InputHandler, to poll input data from the Input Action Asset and the Player Input component. By following these instructions, developers can leverage the Input System's advanced features, which are particularly beneficial when dealing with multiple input device types, including controllers. The author concludes by inviting readers to support their work through membership or direct donations, and provides links to additional resources and personal social media profiles for further engagement.

Opinions

  • The author suggests that the new Input System is superior to the traditional Input Manager due to its robustness and flexibility in handling various input devices.
  • The guide is tailored for Unity versions 2020 and 2021, implying that the steps are relevant and applicable to the latest versions of the Unity engine.
  • The author expresses a preference for creating a default Input Action Asset from the Player Input system to avoid starting from scratch, indicating a convenience factor for users.
  • By instructing readers to change the Player Input component's behavior to "Invoke C# Events," the author conveys a best practice for integrating the Input System with C# scripts in Unity.
  • The article provides a subjective comparison between the Input System and the Input Manager, with the former being described as more complex but offering a more comprehensive solution.
  • The author encourages reader support for their educational content, indicating a commitment to continuing the production of articles and videos on Unity development.

Unity: Handling Input With the Input System

The Input System is an incredibly robust solution to handling input between multiple devices. There are a quite a few more steps to using the Input System when compared to the Input Manager which I will step through here. These steps are demonstrated using the 2020 version, although they are the same in the 2021 version as well.

The Unity Input System allows you to access the input through multiple methods. Here, I am going to demonstrate one way to use the Input System through C# commands and a custom input poller.

The Steps

  1. Get the Input System package installed
  2. Create an Input Action Asset
  3. Add a Player Input component to a game object
  4. Poll the Input System with our custom code

Installing the input system package

To do this you navigate to the Window drop down, then to the Package Manager option.

Getting to the Package Manager in Unity 2020

From there we need to navigate to the Unity Registry. This is where the Unity built packages are held.

Navigating to the Unity Registry inside the Package Manager

All we need to do now is find the Input System package and install it. Unity will then prompt us to restart the Unity engine. Click Yes and allow Unity to restart.

Installing the Input System from the Package Manager
Allowing Unity to restart

That's it! You now have successfully installed the Input System.

Creating the Input Action Asset and Adding the Player Input Component

These two steps can be done in any order, however, if you don’t like starting from scratch every time then Unity allows you to create a default Input Action Asset from the Player Input system.

To demonstrate this we are going to add the Player Input component to a GameObject in our scene.

Adding the Player Input component to a GameObject
Creating the default Input Action Asset

Before we go any further, be sure to change the Behavior from “Send Messages” to “Invoke C# Events”

Changing the Behavior of the Player Input component

Now we have both the Player Input component and an Input Action Asset created!

Polling the Input System with our custom code

This next step will allow us to read the input from the Input Action Asset and the Player Input component. To follow along directly please keep all the names the same and use the default Input Action Asset.

Creating the Script

We are going to create a new script called InputHandler .

Create the c# script in Unity
Input Handler Script

To access the Input System you have to use the namespace UnityEngine.InputSystem; at the top of the script.

The logic of this script is to create public variables that other classes can utilize while also creating private variables for accessing the Input Action Asset through the Player Input component. the private variables will be of type InputAction.

new InputHandler script to handle the Input System

Now we need to just hook up the references to the scripts and we are good to go!

First we will assign the InputHandler reference to the PlayerInput component. Then we will allow the Player script to access the InputHandler script

We are going to add a reference to the Player class to allow it to read the InputHandler, then assign the move input to our Update script.

Make sure you assign the reference for the Player class to access the InputHandler script!

Assigning the InputHandler Reference

Hit Play and test it out!

Movement using the Input System

These are definitely more steps and moving parts than the Input Manager solution, however, with these more steps comes a much more robust solution when working with controllers and switching between input device types.

The Author

If you would like to support me so that I can continue putting out articles and videos please consider becoming a member. If you would like to give a more direct donation, feel free to buy me a coffee Ko-Fi/MikeQ. If you have any questions, complaints, or funny jokes, be sure to throw them in the comments.

Want to know more about me? Feel free to checkout my latest interview with BadVR or connect with me on LinkedIn.

Unity
Unity Input System
Game Development
Gamedev
Beginner
Recommended from ReadMedium