docs.unity3d.com
    Show / Hide Table of Contents

    Quick start guide

    Note: For information on how to install the new Input System, see Installation.

    • Quick start guide
      • Getting input directly from an Input Device
      • Getting input indirectly through an Input Action
        • Step 1: Add a PlayerInput Component
        • Step 2: Create Input Actions
        • Step 3: Setting up Action responses
      • Alternate ways to set up Input Actions

    Getting input directly from an Input Device

    The quickest way to get started in script is to read the current state directly from Input Devices. For example, the following code gets the gamepad that a player last used, and reads its current state:

    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class MyPlayerScript : MonoBehaviour
    {
        void Update()
        {
            var gamepad = Gamepad.current;
            if (gamepad == null)
                return; // No gamepad connected.
    
            if (gamepad.rightTrigger.wasPressedThisFrame)
            {
                // 'Use' code here
            }
    
            Vector2 move = gamepad.leftStick.ReadValue();
            // 'Move' code here
        }
    }
    

    The same approach works for other Device types (for example, Keyboard.current or Mouse.current).

    Getting input indirectly through an Input Action

    To get input directly through an Input Action, follow these steps:

    1. Add a PlayerInput component.
    2. Create Actions.
    3. Script Action responses.

    Step 1: Add a PlayerInput Component

    Getting input directly from an Input Device is quick and convenient, but requires a separate path for each type of Device. That also makes it harder to later change which Device Control triggers a specific event in the game.

    Alternatively, you can use Actions as an intermediary between Devices and the in-game responses they trigger. The easiest way to do this is to use the PlayerInput component. To add this component, click the Add Component button in the GameObject Inspector:

    Add Player Input Component

    Step 2: Create Input Actions

    Each PlayerInput component represents one player in the game. To receive input, the component must be connected to a set of Input Actions. The quickest way to create a new set of Actions is to click the Create Actions… button in the Inspector window for that component. This creates an Asset pre-populated with a default set of Input Action Maps, Input Actions, and Input Bindings.

    Create Actions from Player Input Component

    When you click the Create Actions button, Unity asks you where to create the new Asset. Choose a name and folder inside the Assets folder of your Project (or just accept the defaults) and select Okay. This creates a new .inputactions Asset in your Project, connects it to the PlayerInput component, and brings up the editor window for .inputactions files.

    MyGameActions

    You can edit the default set to fit the needs of your Project. See the in-depth documentation for the Action editor for instructions on how to use this window.

    Step 3: Setting up Action responses

    Once the component has its Actions, you must set up a response for each Action. PlayerInput allows you to set up responses in several ways, using the Behavior property in the Inspector window:

    PlayerInput Notification Behavior

    For more details about the options, see documentation on notification behaviors. The screenshot above uses Invoke Unity Events, which uses UnityEvent in the same way the Unity UI does. Unity displays an event for each Action that is linked to the component. This allows you to directly wire in the target method for each event.

    PlayerInput Action Events

    Each method takes an InputAction.CallbackContext argument that gives access to the Control that triggered the Action and the Action's value. For more information, see documentation on Action callbacks.

    public class MyPlayerScript : MonoBehaviour
    {
        public void Fire(InputAction.CallbackContext context)
        {
            Debug.Log("Fire!");
        }
    }
    

    This completes the basic setup using PlayerInput.

    Alternate ways to set up Input Actions

    There are ways other than PlayerInput to set up Input Actions. For more information, see documentation on Creating Actions.

    In This Article
    • Getting input directly from an Input Device
    • Getting input indirectly through an Input Action
      • Step 1: Add a PlayerInput Component
      • Step 2: Create Input Actions
      • Step 3: Setting up Action responses
    • Alternate ways to set up Input Actions
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023