CodeBlanks Logo Icon CodeBlanks

Input Action asset

Learn to create an Input Action asset and define actions.

What is an Input Action asset?

An Input Action asset is an asset that keeps actions and bindings in one file; its extension is .inputactions. You create it with Create > Input Actions in the Project window, and double-clicking opens the Input Actions editor.

{
    "name": "Jump",
    "type": "Button",
    "bindings": [
        { "path": "<Keyboard>/space" }
    ]
}

The file content is JSON, so it is easy to track with version control.

Action Map, Action and Binding

An Action Map collects the actions of one game state; for example the Player map holds movement and jumping. An action is a single input function and its type is Button, Value or Pass Through. A binding connects the action to a physical control, and composites like 2DVector merge the WASD keys into a single Vector2 value.

moveAction.AddBinding("<Gamepad>/leftStick");
moveAction.AddCompositeBinding("2DVector")
    .With("Up", "<Keyboard>/w");

Practice Define a Jump action

Write a MonoBehaviour named EylemTanimla. In Start create an InputAction named Jump with type InputActionType.Button. Declare a string array named yollar holding <Keyboard>/space and <Gamepad>/buttonSouth. Loop over the array with foreach; for each path call jump.AddBinding(yol) and print it with Debug.Log("Baglama: " + yol). Finally call jump.Enable().

Starter code

Player.cs
using UnityEngine;
using UnityEngine.InputSystem;

public class EylemTanimla : MonoBehaviour
{
    void Start()
    {
        // Adi Jump, tipi Button olan bir InputAction olustur

        // yollar dizisini iki baglama yoluyla doldur

        // foreach ile her yol icin AddBinding cagir ve yazdir

        // eylemi etkinlestir
    }
}

Sample solution

Player.cs
using UnityEngine;
using UnityEngine.InputSystem;

public class EylemTanimla : MonoBehaviour
{
    void Start()
    {
        InputAction jump = new InputAction(name: "Jump", type: InputActionType.Button);
        string[] yollar = { "<Keyboard>/space", "<Gamepad>/buttonSouth" };

        foreach (string yol in yollar)
        {
            jump.AddBinding(yol);
            Debug.Log("Baglama: " + yol);
        }

        jump.Enable();
    }
}

Expected output

Baglama: <Keyboard>/space
Baglama: <Gamepad>/buttonSouth

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What binding path connects the space key to the Jump action?

  • <Mouse>/leftButton
  • <Keyboard>/space Correct answer
  • <Keyboard>/escape
  • <Gamepad>/buttonSouth

Explanation: A binding path joins the device name and the control name; the space key is the space control of the Keyboard device, so the path is <Keyboard>/space.

2 Bind the left analog stick

Question
moveAction.AddBinding("____");
Answer
moveAction.AddBinding("<Gamepad>/leftStick");

Explanation: The path of the left analog stick for movement is <Gamepad>/leftStick; the right stick is used for the camera and buttonSouth is the A/Cross button.

3 Action asset parts

Term Meaning
Action Map The section that groups actions
Action A single input function
Binding Link from action to physical control
Composite WASD keys merging into one action

All lessons in Advanced