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
MediumGuard the vault with a laser tripwire.
- Each frame, cast
Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, range, intruders). - Colour
lamp.material.colorColor.redwhile it hits andColor.greenwhile it is clear. - Log
"Alarm: " + hit.collider.nameonly when the beam goes from clear to blocked: keep last frame's answer in abool blockedfield.
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()
{
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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