Menu
CoddyTech

Input Actions

Part of the Building Real Games section of Coddy's Unity journey. Lesson 46 of 56.

Testing keys one by one ties a script to the keyboard. An action names what the player does instead, and the project lists which keys, sticks and buttons trigger it. A new Unity 6 project ships with a set of them (the InputSystem_Actions asset, set as the project-wide actions), whose Player map has Move (WASD, the arrows and a gamepad's left stick), Look (the mouse or the right stick), Attack (the left mouse button and Enter), Interact (E, held), Crouch (C), Jump (Space and the gamepad's south button), Previous and Next (1 and 2) and Sprint (Left Shift). They are switched on from the start, and InputSystem.actions.FindAction("Jump") gets one by name. This side-view claw crane moves on x and y and grabs on Jump:

using UnityEngine;
using UnityEngine.InputSystem;

public class ClawCrane : MonoBehaviour
{
    public float speed = 3f;

    private InputAction moveAction;
    private InputAction jumpAction;

    void Start()
    {
        moveAction = InputSystem.actions.FindAction("Move");
        jumpAction = InputSystem.actions.FindAction("Jump");
    }

    void Update()
    {
        Vector2 input = moveAction.ReadValue<Vector2>();
        transform.position += new Vector3(input.x, input.y, 0f) * speed * Time.deltaTime;

        if (jumpAction.WasPressedThisFrame())
            Debug.Log("Grab!");
    }
}

The actions are found once, in Start, and kept in InputAction fields. Move gives a Vector2: x is right and left, y is up and down on the keys, and it is normalized, so W and D together give (0.71, 0.71) rather than (1, 1). The crane is a side view, so it uses that y for height; a game seen from above would use it for z. Button actions answer like keys: WasPressedThisFrame(), WasReleasedThisFrame() and IsPressed(). Two mistakes throw: a misspelled name makes FindAction return null (a NullReferenceException on the first read), and ReadValue<Vector3>() on Move throws an InvalidOperationException as soon as a key is held, because Move holds a Vector2.

challenge icon

Challenge

Medium

The surveyor maps a snowfield, seen from above, and carries flags (3) to plant on the way. The script has speed (4), flags, flagPrefab and a list planted. Use the project's actions:

  • Move walks the surveyor across the snow at speed units per second: right and left along x, up and down along z.
  • Each press of Jump plants a flag: a copy of flagPrefab at the surveyor's x and z, at y 0.75, added to planted. It uses up one of flags and logs what is left, such as Flags left: 2. With no flags left, Jump plants nothing.
  • Each press of Attack (a left click) pulls up the flag planted last: it is destroyed and taken off the list, flags goes up by one, and the same line is logged.

Press Play, plant three flags and pull one up, then Check.

Try it yourself

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Surveyor : MonoBehaviour
{
    public float speed = 4f;
    public int flags = 3;
    public GameObject flagPrefab;

    private List<GameObject> planted = new List<GameObject>();

    void Start()
    {
        // find the Move, Jump and Attack actions
    }

    void Update()
    {
        // walk with Move, plant a flag on Jump, pull one up on Attack
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Building Real Games