Unity 2024 Input System Tutorial: Basic Platformer Movement
The Unity Input System offers a flexible way to handle player input, including keyboard, mouse, and controller support. In this tutorial, we’ll implement basic 2D platformer movement using Unity’s new Input System. You’ll learn how to set up a simple movement script and configure the Input System to handle user input efficiently.
You will need to use Unity 2021 or higher.
Step 1: Install the Input System Package
If you haven’t installed the Input System package yet, follow these steps:
- Go to Window → Package Manager.
- In the Package Manager, search for “Input System” and install it.
- Once installed, Unity will prompt you to restart the editor and enable the new input system.
- Choose Yes.
Step 2: Set Up Input Actions
Create a New Input Action Asset:
- Right-click in your Assets folder and select Create → Input Actions.
- Name it PlayerControls or something similar.
Open the Input Action Window:
- Double-click the newly created PlayerControls asset to open the Input Action editor.
Create a New Action Map:
- In the editor window, create a new action map called “Player”.
Add Movement Action:
- Under the “Player” action map, create a new action named Move.
- Set its action type to Value and control type to Vector2 (since we’ll be reading both horizontal and vertical movement).
Assign Input Bindings:
- Click the Move action, then click Add Binding and select 2D Vector Composite.
Add the following bindings:
- Up: W key
- Down: S key
- Left: A key
- Right: D key
This setup allows the player to move using the WASD keys on the keyboard.
Add Jump Action:
Note: If you want the jump key to be W, simply remove the Up binding from the step above and change this binding from Space to W key
Create another action in the “Player” action map called Jump.
- Set its action type to Button and add a binding for the Space key.
- Save the asset after all bindings are set up.
Step 3: Generate a C# Class from the Input Action
In the Input Actions editor, click the “Generate C# Class” button at the top of the window.
Choose a save location for your script, and Unity will generate a PlayerControls.cs file based on your input actions.
Step 4: Create the Player Movement Script
Now, let’s create the actual movement script that will use the input we just set up.
Create a new script called PlayerMovement.cs and attach it to your player GameObject.
Open the script and replace its content with the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private PlayerControls controls;
private Rigidbody2D rb;
private Vector2 moveInput;
private bool isJumping = false;
[Header("Movement Settings")]
public float moveSpeed = 5f;
public float jumpForce = 7f;
public LayerMask groundLayer;
private void Awake()
{
// Initialize the PlayerControls instance
controls = new PlayerControls();
// Subscribe to the movement and jump actions
controls.Player.Move.performed += ctx => moveInput =
ctx.ReadValue<Vector2>();
controls.Player.Move.canceled += ctx => moveInput =
Vector2.zero;
controls.Player.Jump.performed += ctx => Jump();
}
private void OnEnable()
{
// Enable the input controls
controls.Enable();
}
private void OnDisable()
{
// Disable the input controls when the player object is disabled
controls.Disable();
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
Move();
}
private void Move()
{
// Apply horizontal movement based on input
rb.velocity = new Vector2(moveInput.x * moveSpeed, rb.velocity.y);
}
private void Jump()
{
// Check if the player is grounded before jumping
if (IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
private bool IsGrounded()
{
// Use a simple ground check to see if the player is touching
// the ground
RaycastHit2D hit = Physics2D.Raycast
(transform.position, Vector2.down, 0.1f, groundLayer);
return hit.collider != null;
}
}
Step 5: Set Up the Player GameObject
Create a 2D Player Object:
- In your scene, create a 2D Sprite object for the player.
- Add a Rigidbody2D component to the player and set the body type to Dynamic.
- Add a BoxCollider2D to represent the player’s hitbox.
- Set Ground Layer:
Create a new layer called “Ground” and assign this layer to any objects the player will collide with (e.g., platforms).
Assign LayerMask:
In the PlayerMovement script, set the Ground Layer field in the inspector to the “Ground” layer.
Step 6: Test the Game
Hit the Play button to test your platformer! You should be able to move left and right using WASD or arrow keys, and jump using the spacebar.
This will give you the basis for a functional 2D platformer movement system with Unity’s new Input System. This setup allows for easy customization and scalability, supporting multiple input devices with minimal adjustments. Experiment with the script to add more mechanics, and happy coding!