0
0
Unityframework~5 mins

Input action maps in Unity

Choose your learning style9 modes available
Introduction

Input action maps help organize and manage different player controls in a game. They group related input actions so you can easily switch or handle controls.

You want to separate controls for walking, running, and jumping.
You need different controls for menus and gameplay.
You want to support multiple control schemes like keyboard and gamepad.
You want to enable or disable certain controls during specific game states.
You want to keep input code clean and easy to manage.
Syntax
Unity
InputActionMap actionMap = new InputActionMap("PlayerControls");
actionMap.AddAction("Jump", binding: "space");
actionMap.Enable();
An InputActionMap groups multiple InputActions under one name.
You enable the whole map to listen for its actions.
Examples
This creates a map named 'Gameplay' with Move and Jump actions bound to keyboard keys.
Unity
var actionMap = new InputActionMap("Gameplay");
actionMap.AddAction("Move", binding: "<Keyboard>/w");
actionMap.AddAction("Jump", binding: "<Keyboard>/space");
actionMap.Enable();
This map handles menu navigation using a gamepad.
Unity
var menuMap = new InputActionMap("Menu");
menuMap.AddAction("Navigate", binding: "<Gamepad>/dpad");
menuMap.AddAction("Select", binding: "<Gamepad>/buttonSouth");
menuMap.Enable();
Sample Program

This script creates a Player input action map with a Jump action bound to the spacebar. When space is pressed, it logs "Jump pressed!" to the console.

Unity
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInputExample : MonoBehaviour
{
    private InputActionMap playerActionMap;
    private InputAction jumpAction;

    void Awake()
    {
        playerActionMap = new InputActionMap("Player");
        jumpAction = playerActionMap.AddAction("Jump", binding: "<Keyboard>/space");
        jumpAction.performed += ctx => Debug.Log("Jump pressed!");
    }

    void OnEnable()
    {
        playerActionMap.Enable();
    }

    void OnDisable()
    {
        playerActionMap.Disable();
    }
}
OutputSuccess
Important Notes

Always enable your InputActionMap to start receiving input events.

You can disable maps to temporarily stop input without removing actions.

Use descriptive names for maps and actions to keep your code clear.

Summary

Input action maps group related input actions for better control management.

Enable or disable maps to control when inputs are active.

Bind actions to keys or buttons inside maps to handle player input cleanly.