Input System

Learn how Unity’s input system can help create responsive and interactive experiences.

Introduction

In game development, user input is a fundamental aspect that allows players to interact with and control the game. Unity provides two input systems: the old Input System package and the new Input System package. In this lesson, we'll explore both input systems, their features, and how to use them in our Unity projects.

The old Input System package

The old Input System is the legacy input system in Unity. It has been widely used in previous versions and is still compatible with the latest Unity releases. This input system relies on the Input class and provides various methods to handle user input, such as GetKey, GetButton, and GetAxis. These methods allow us to detect keyboard, mouse, and controller inputs. The old Input System is straightforward and suitable for simple input requirements.

Using the old Input System in Unity

In this example, we’ll demonstrate how to use Unity’s old Input System to detect keyboard input for player movement in a 3D environment. We’ll create a scene where a player object moves horizontally using the arrow keys.

The step-by-step instructions

  1. Scene setup:

  • Begin by creating a new scene in Unity.

  • Set up a scene with a simple 3D character representing the player. This can be a cube.

  • Ensure that the player object is on a flat surface for movement. We can use a plane from the same menu we created a cube.

  1. Create a script:

  • Create a new C# script in our project named PlayerMovement.

  • Open the script in the preferred code editor.

Press + to interact
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Declare a public variable for the player's movement speed.
public float speed;
// Update is called once per frame.
void Update()
{
// Get the horizontal input axis value (left or right arrow keys).
float moveInput = Input.GetAxis("Horizontal");
// Calculate the player's movement in the horizontal direction.
Vector3 movement = new Vector3(moveInput * speed * Time.deltaTime, 0f, 0f);
// Move the player object based on the calculated movement.
transform.position += movement;
}
}
  1. Attach the script to the player object:

  • In the Unity ...