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
HardWire up the plates and the code: 1, 2, 3.
- In
Plate, when thePlayerenters, callcodeLock.Press(number);. - In
Press, withint next = 1: a match logs"Plate " + numberand adds 1; after 3, logUnlocked, resetnext. - Else log
Wrong plate, resetnext, run a coroutine: platesColor.red, 1 second, thenColor.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
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable WallPractice on your own: Unity playground