Menu
CoddyTech

Escape Room

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 70 of 71.

An escape room with a code lock. Three gray pressure plates lie on the floor, and the door in the north wall opens only when the player steps on them in the right order: plate 1, plate 2, plate 3. Nothing in the room says which plate is which. A wrong plate starts the code over and flashes every plate red for a second. The open door stays open for a few seconds, then shuts again, but never on the player: while the player stands in the doorway, it waits.

The Player script is ready: the arrows walk the player, which is tagged Player and sits on the Player layer. Write two scripts: Plate on each plate and CodeLock on the Lock.

challenge icon

Challenge

Hard

Wire up the plates and the code: 1, 2, 3.

  1. In Plate, when the Player enters, call codeLock.Press(number);.
  2. In Press, with int next = 1: a match logs "Plate " + number and adds 1; after 3, log Unlocked, reset next.
  3. Else log Wrong plate, reset next, run a coroutine: plates Color.red, 1 second, then Color.gray.

Press Play and find the order. Then Check.

Try it yourself

using System.Collections;
using UnityEngine;

public class CodeLock : MonoBehaviour
{
    [SerializeField] private Renderer[] plates;
    [SerializeField] private GameObject door;
    [SerializeField] private Transform doorway;
    [SerializeField] private float openTime = 4f;

    public void Press(int number)
    {
        // check the code; flash the plates on a wrong one; open the door after plate 3
    }
}

All lessons in Physics and Gameplay

Practice on your own: Unity playground