Menu
CoddyTech

Recap: Laser Tripwire

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

This recap puts the chapter together: a raycast that sees only some layers, what it hit, and a bool that remembers last frame so the alarm fires once.

challenge icon

Challenge

Medium

Guard the vault with a laser tripwire.

  1. Each frame, cast Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, range, intruders).
  2. Colour lamp.material.color Color.red while it hits and Color.green while it is clear.
  3. Log "Alarm: " + hit.collider.name only when the beam goes from clear to blocked: keep last frame's answer in a bool blocked field.

Press Play and walk through the beam: one alarm each time. Then Check.

Try it yourself

using UnityEngine;

public class Tripwire : MonoBehaviour
{
    public LayerMask intruders;
    public Renderer lamp;
    public float range = 6f;

    void Update()
    {
    }
}
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

Practice on your own: Unity playground