Menu
CoddyTech

Recap: Laser Tripwire

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

challenge icon

Challenge

Medium

Guard the vault with a laser tripwire. The Tripwire script sits on the Emitter post, and the Inspector gives it intruders (a LayerMask with the Player layer ticked), the lamp (a Renderer) and a range of 6.

  • The beam is a ray from the Emitter along its own forward, range units long, that sees only the intruders layers. The Receiver post and the crates are on other layers.
  • While something blocks the beam, the lamp is red (Color.red); while the beam is clear, it is green (Color.green).
  • Each time the beam goes from clear to blocked, log Alarm: Player, with the name of what blocked it. Standing in the beam does not log again; leaving and coming back does.

Press Play and walk the player through the beam with the arrows, then Check.

Try it yourself

using UnityEngine;

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

    void Update()
    {
    }
}

All lessons in Physics and Gameplay