Line of Sight
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 37 of 63.
Being close to a guard is not the same as being seen by it: a wall in between blocks the view. To test line of sight, cast a ray from the eye toward the target, and check that the first thing it hits is the target itself. The direction is the target's position minus your own, and a ray that long reaches the target:
public Transform target;
bool CanSee()
{
Vector3 toTarget = target.position - transform.position;
if (Physics.Raycast(transform.position, toTarget, out RaycastHit hit, toTarget.magnitude))
{
return hit.transform == target;
}
return false;
}A security camera on a pillar logs I see you while CanSee() is true. With the target in the open, the ray's first hit is the target. With the target behind a crate, the ray hits the crate first, hit.transform is the crate's, and the answer is false. Physics.Raycast on its own returns true for the crate too, which is why the hit is compared with the target.
The target needs a collider for the ray to find it. The ray needs no normalized direction, since its length comes from maxDistance. Physics.Linecast(from, to) asks the question the other way round, whether any collider lies between two points: !Physics.Linecast(eye, target.position, wallMask) means no wall is in the way.
Challenge
MediumThe sentry tower watches the player. The Sentry script sits on the tower, and the Inspector gives it the player and a range of 8. The tower's colour shows what it sees:
- While it can see the player, colour the tower red (
Color.red), and green (Color.green) otherwise. - It sees the player only when the player is no more than
rangeunits away and nothing stands in between.
Press Play and walk the player around the wall with the arrows, then Check.
Try it yourself
using UnityEngine;
public class Sentry : MonoBehaviour
{
public Transform player;
public float range = 8f;
void Update()
{
// Red while the sentry can see the player, green otherwise.
}
}
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 Pad2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall