DEV Community

Alok Krishali
Alok Krishali

Posted on

How to Implement Unity New Input System for Smooth Gameplay

Introduction

The Unity New Input System is a powerful tool that enhances flexibility and control in handling user inputs. Whether you are developing a simple 2D platformer or a complex 3D shooter, Unity’s New Input System allows for precise and scalable input management. In this guide, we’ll walk you through how to work on Unity New Input System, why it’s a great choice, and how to implement it for smooth gameplay.

Why Unity New Input System?

If you’re wondering why Unity New Input System is preferred over the old Input Manager, here are some key reasons:

More Flexibility: Allows binding multiple inputs to a single action, making remapping easy.

Device Agnostic: Supports controllers, keyboards, touchscreens, and more without extra coding.

Event-Driven: Uses callbacks rather than checking inputs every frame, improving performance.

Better Multiplayer Support: Handles multiple players and devices efficiently.

Customizability: Provides an easy way to extend and modify input behavior.

Setting Up Unity’s New Input System

Before implementing the Unity New Input System, make sure you have it installed and set up in your project.

Step 1: Install the New Input System
Open Unity Package Manager (Window > Package Manager).

Search for Input System.

Click Install.

Once installed, go to Edit > Project Settings > Player and change the Active Input Handling to Both or New Input System.

Restart Unity to apply changes.

Step 2: Creating an Input Actions Asset

In the Project window, right-click and select Create > Input Actions.

Name the asset (e.g., PlayerControls).

Open the asset and click Create Action Map (e.g., Player).

Click Add Action and define your inputs (e.g., Move, Jump, Attack).

Assign controls to each action, such as WASD for movement or Spacebar for jumping.

Save the asset.

Step 3: Generate a C# Script for Input Handling

Select your Input Actions Asset.

Click Generate C# Class and name it PlayerInputActions.

Unity will generate a script that you can use in your code.

Implementing Input in a Player Controller

Now that we have the input system set up, let's implement it in a player script.

Step 1: Create a Player Controller Script

Create a new C# script and name it PlayerController.cs. Open it and add the following code:

`using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
private PlayerInputActions inputActions;
private Vector2 movementInput;
private Rigidbody rb;

public float speed = 5f;


private void Awake()
{
    inputActions = new PlayerInputActions();
    rb = GetComponent<Rigidbody>();
}

private void OnEnable()
{
    inputActions.Player.Enable();
    inputActions.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
    inputActions.Player.Move.canceled += ctx => movementInput = Vector2.zero;
}

private void OnDisable()
{
    inputActions.Player.Disable();
}

private void FixedUpdate()
{
    Vector3 move = new Vector3(movementInput.x, 0, movementInput.y) * speed * Time.fixedDeltaTime;
    rb.MovePosition(rb.position + move);
}
Enter fullscreen mode Exit fullscreen mode

}`

Step 2: Assign the Script to Your Player

Attach the PlayerController script to your player GameObject.

Ensure the player has a Rigidbody component.

Assign movement actions in the Input Actions Asset.

Step 3: Handling Jump and Attack Actions

To extend the functionality, modify the script to include jumping and attacking:

`private void OnEnable()
{
inputActions.Player.Enable();
inputActions.Player.Move.performed += ctx => movementInput = ctx.ReadValue();
inputActions.Player.Move.canceled += ctx => movementInput = Vector2.zero;
inputActions.Player.Jump.performed += ctx => Jump();
inputActions.Player.Attack.performed += ctx => Attack();
}

private void Jump()
{
rb.AddForce(Vector3.up * 5f, ForceMode.Impulse);
}

private void Attack()
{
Debug.Log("Attack!");
}`

Testing and Debugging Your Input System

Debugging Tips

Use Debug.Log() to check input values.

Ensure Input Actions Asset is correctly referenced in the script.

If inputs are not responding, check the Active Input Handling setting in Project Settings.

Make sure the Player Input Component is active on your player GameObject.

Conclusion

Implementing Unity's New Input System improves input management and scalability. By following this guide, you can set up and integrate inputs efficiently into your game, ensuring smooth gameplay. Whether you are developing for PC, mobile, or console, Unity New Input System offers an adaptable and modern solution. Start using it today to enhance your game’s responsiveness and player experience!

Top comments (0)