Menu
CoddyTech

Keyboard and Mouse

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

The Input System gives each device an object. Keyboard.current is the keyboard, and every key on it is a control with a name: spaceKey, aKey, upArrowKey, or Keyboard.current[Key.Space] for any key by its Key value. Mouse.current is the mouse, with leftButton, rightButton and position. A control's isPressed is true on every frame it is held down, as Input.GetKey was. This submarine dives and surfaces with the arrows and shines its searchlight while the mouse button is held:

using UnityEngine;
using UnityEngine.InputSystem;

public class Submarine : MonoBehaviour
{
    public float diveSpeed = 2f;
    public Light searchlight;

    void Update()
    {
        Keyboard keyboard = Keyboard.current;
        if (keyboard == null) return;   // no keyboard plugged in

        if (keyboard.downArrowKey.isPressed)
            transform.position += Vector3.down * diveSpeed * Time.deltaTime;
        if (keyboard.upArrowKey.isPressed)
            transform.position += Vector3.up * diveSpeed * Time.deltaTime;

        searchlight.enabled = Mouse.current != null && Mouse.current.leftButton.isPressed;
    }
}

current is the device used last, and it is null when there is none: a phone or a console may have no keyboard, and the null checks keep the script from throwing there. The keyboard has no axes, so the down arrow and S are two separate keys: a submarine that should also dive on S tests keyboard.downArrowKey.isPressed || keyboard.sKey.isPressed. Mouse.current.position.ReadValue() gives the pointer as a Vector2 in screen pixels from the bottom left corner, like Input.mousePosition.

challenge icon

Challenge

Medium

Fly the hot air balloon with the Input System. Its script has drift (2), rise (3), sink (1), groundY (2) and flame, the burner's flame under the balloon.

  • While the right arrow or D is held, the balloon drifts right (along +x) at drift units per second; while the left arrow or A is held, it drifts left.
  • The burner is on while Space or the left mouse button is held. Show flame only while it is on.
  • With the burner on, the balloon rises at rise units per second. With it off, it sinks at sink units per second, and never below groundY.

Use Keyboard.current and Mouse.current. This project is set up like a new Unity 6 project (Active Input Handling: Input System Package), so the Input class throws an InvalidOperationException here. Press Play and fly, then Check.

Try it yourself

using UnityEngine;
using UnityEngine.InputSystem;

public class Balloon : MonoBehaviour
{
    public float drift = 2f;
    public float rise = 3f;
    public float sink = 1f;
    public float groundY = 2f;
    public GameObject flame;

    void Update()
    {
        // drift with the arrows or A and D, burn with Space or the mouse
    }
}
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