Menu
CoddyTech

Escape Room

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

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

Plate, on each plate (a trigger). The Inspector gives it number (1, 2 or 3) and codeLock (the Lock's CodeLock script).

  • When the player (tagged Player) steps onto the plate, call codeLock.Press(number). Anything else landing on it does nothing.

CodeLock, on the Lock. The Inspector gives it plates (the three plates' Renderers, in order), door (the Door), doorway (a point in the middle of the doorway) and openTime (4).

  • A public method Press(int number). When number is the plate the code expects next (1, then 2, then 3), log it, as Plate 2. When that was plate 3, also log Unlocked, start the code over from plate 1, and start a coroutine that opens the door.
  • Any other plate logs Wrong plate, starts the code over from plate 1, and starts a coroutine that turns every plate red (Color.red) for 1 second, then back to gray (Color.gray).
  • The door coroutine deactivates the door and waits openTime seconds. Then it keeps waiting, a frame at a time, while anything on the Player layer is within 1 unit of doorway. Last, it activates the door again and logs Door closed.

Press Play, find the order and get out, 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
    }
}
quiz iconTest yourself

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

All lessons in Physics and Gameplay