Course contentsShow
← Unity Game Development
Lesson 174 of 8283. Physics and InteractionFree lesson

Managing Contextual Input Action Maps

Switch between different input action maps to handle distinct control contexts such as gameplay, menu navigation, and vehicle operation.

In this lesson, you will learn how to compartmentalize your game controls by switching between distinct Input Action Maps based on the player's current context.

Why Use Contextual Maps?

As your game grows, you will realize that "walking" controls and "menu" controls often conflict. If your Jump button is also your Select button, pressing it while in a menu might accidentally make your character leap into a hazard. By using Action Maps, you can define separate buckets of input logic—like "Player" for movement and "UI" for menus—and swap them instantly.

Implementing the Switch

When you have a PlayerInput component attached to your object, you can transition between these maps using the SwitchCurrentActionMap method. This ensures that only the actions relevant to the current state are active.

Imagine you are walking around, but you press a key to open an inventory menu. You would use code like this:

using UnityEngine;
using UnityEngine.InputSystem;

public class InputManager : MonoBehaviour
{
    private PlayerInput playerInput;

    void Awake() 
    {
        playerInput = GetComponent<PlayerInput>();
    }

    public void OpenMenu() 
    {
        // Disable gameplay controls and switch to menu navigation
        playerInput.SwitchCurrentActionMap("Menu");
    }

    public void CloseMenu() 
    {
        // Return to gameplay movement controls
        playerInput.SwitchCurrentActionMap("Player");
    }
}

The Power of Separation

By partitioning your controls, you avoid "input bleed." When the "Player" map is active, the Input System ignores any callbacks linked to the "Menu" map. This keeps your scripts clean; you won't need complex if (isPaused) checks inside your character movement logic. Instead, you simply deactivate the movement map, and the Input System handles the rest by stopping the delivery of those events.

This modular approach ensures that whether your player is driving a vehicle, navigating a UI, or sprinting through a level, your code only responds to the inputs that make sense for that specific situation.

Key Takeaway: Use SwitchCurrentActionMap to toggle between different Input Action Maps, ensuring your game only processes the inputs relevant to the current gameplay context.