Recap: Laser Tripwire
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 39 of 63.
Challenge
MediumGuard 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,
rangeunits long, that sees only theintruderslayers. 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
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall6Raycasts
Physics.RaycastRaycastHitGround Check with a RayLayers and LayerMaskLine of SightOverlapSphere and SphereCastRecap: Laser Tripwire