06 — First-Person and Weapon Switching

Run Tools → Input System Starter Kit → Create First-Person Demo in an empty scene. WASD to move, mouse to look, Shift to sprint, Space to jump. Click to fire — you paint targets with the current weapon's color. Scroll the wheel or press 1/2/3 to switch between the three primitive "weapons". All of it works on a gamepad too. Two scripts: FirstPersonController.cs and WeaponSwitcher.cs.

Movement and look — FirstPersonController.cs

bool fromStick = lookAction.activeControl?.device is Gamepad;
Vector2 delta = fromStick
    ? look * (stickSensitivity * Time.deltaTime)   // degrees per second
    : look * mouseSensitivity;                     // per-pixel

activeControl tells you which physical control most recently drove the action — the clean way to ask "keyboard or gamepad?" without maintaining device state.

Weapon switching — WeaponSwitcher.cs

Five actions, three techniques:

  1. NextWeapon / PreviousWeapon — bound to <Mouse>/scroll/up and /down (sub-controls of the scroll vector used as buttons) plus the shoulder buttons. The handlers just move an index with wraparound arithmetic.
  2. WeaponSlot1/2/3 — instead of three copy-pasted handlers, a loop subscribes a lambda that captures its slot index:
for (int i = 0; i < 3; i++)
{
    int slot = i;   // capture a COPY — capturing 'i' directly is the classic C# trap
    map.FindAction($"WeaponSlot{i + 1}").performed += _ => Equip(slot);
}

The comment in OnDisable about not being able to unsubscribe those lambdas is deliberate — have students explain why (the delegate was never stored), then fix it by storing the delegates in an array. Ready-made discussion question.

  1. Fire — a performed handler raycasts from the camera center (Physics.Raycast) and recolors what it hits. Hitscan in ten lines; swapping in a projectile prefab is the natural extension.

Exercises

  1. Add hold-to-charge: put a Hold interaction (0.5 s) on Fire; performed now means "charged" and canceled before that means "tap shot". No timers in code.
  2. Add an Interact action (E / buttonWest) that raycasts 2 m and logs what you're looking at — doors and pickups start here.
  3. Give each weapon a different fire behavior (the Zapper paints random colors; the Orb scales targets down). All the input plumbing is already there.

Deeper reference: FPS Controller with the Input System and samyam's Cinemachine first-person variant.

Next: 07 - Puzzle Selection and Drag & Drop