Overview of Unity's New Input System

The New Input System in Unity provides an updated, flexible, and more intuitive approach to handling inputs in games. With this system, developers can cater to a variety of devices, from game controllers to VR handsets, making gameplay more dynamic and immersive.

1. Why the New System

The traditional input system in Unity was limited in its capabilities and not as extensible. The New Input System offers a unified API, addressing previous limitations and providing advanced features for modern game development.

2. Setting Up

To start using the New Input System, it must first be installed from the Package Manager in Unity. Once added, the transition from the old system requires certain adjustments in scripts and settings.

3. Key Features

  • Actions and Bindings: Instead of directly referencing buttons or keys, developers define actions, and then bind those actions to specific keys or buttons.
  • Device Variability: Easily supports multiple devices, from traditional gamepads to touchscreens and VR handsets.
  • Player Controls: Provides a built-in mechanism for rebinding controls, allowing players to set up controls as preferred.
  • Advanced Event Handling:Events can be processed in various ways, whether polling-based or callback-based, granting more control.

4. Creating an Input Action

An Input Action is a new asset type that allows for the definition of input behaviors without having to write scripts. Once created, it can be dropped into a script, connecting gameplay directly to player inputs.

5. Implementation in Scripting

  • The New Input System seamlessly integrates with the Unity scripting environment. Instead of the older 'Input.GetKey' or 'Input.GetAxis' methods, this system uses ´actions´ that can be called within scripts. Check the example below:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
    public float speed = 5.0f;
    private Vector2 moveInput;

    public void OnMove(InputAction.CallbackContext context)
    {
        moveInput = context.ReadValue<Vector2>();
    }

    void Update()
    {
        Vector3 move = new Vector3(moveInput.x, 0, moveInput.y) * speed * Time.deltaTime;
        transform.Translate(move);
    }
}

Let’s break down the provided code example above:

  • Namespaces: The 'UnityEngine.InputSystem' namespace is essential in order to use the new functionalities.
  • 'moveInput': This variable stores the input values, commonly representing horizontal and vertical inputs.
  • 'OnMove()': Defined as a public function, 'OnMove' can be linked directly to an Input Action in the Unity Editor. The method utilizes 'InputAction.CallbackContext' to read the input values and assign them to our 'moveInput' variable.
  • 'Update()': In the 'Update' method, the stored input values determine the movement direction. The input is translated into a 3D Vector for character movement, factoring in the defined speed and the Unity 'Time.deltaTime' for frame-independent movement.

To make this script work, it’s important to link the 'OnMove' method to an Input Action via the Unity Editor. This connection ensures that when a player provides input (like moving a joystick or pressing arrow keys), the corresponding action triggers the desired script function.

Conclusion

The New Input System in Unity is a powerful and necessary evolution, aligning with the needs of modern game development. It offers versatility, precision, and user customization, making game interactions more engaging and responsive.

Suggested Articles
Fishing Game Guide for Unity
Creating a Simple Platformer Game in Unity
How to Create a Quiz Game in Unity
Building a Top-Down Shooter Game in Unity
Creating 2D Animations in Unity
Introduction to Unity's Animation System
Introduction to Particle System in Unity