Enemy Vision in Unity: Field of View, Line of Sight, and Fair Detection
Design reliable enemy vision in Unity with field-of-view checks, line-of-sight occlusion, focused and peripheral cones, awareness buildup, and fair loss of sight.
Treat vision as four separate questions
A dependable vision sensor should not begin with a single raycast. It should answer four questions in order: is the target close enough to matter, is it inside a field of view, is there an unobstructed path to a useful sample point, and how quickly should that evidence change the guard state?
Keeping those questions separate makes the system easier to tune and debug. Range and angle cheaply reject most candidates. Occlusion is only tested for the small set that remains. Awareness then turns a geometric result into gameplay instead of forcing an instant spotted-or-hidden decision.
- Range: reject targets outside the sensor radius.
- Angle: distinguish focused vision from peripheral awareness.
- Occlusion: test from the guard eye position to a meaningful target point.
- Awareness: accumulate and decay evidence over time.
Use focused and peripheral vision for better stealth
A single hard cone creates obvious edge cases. A player one degree outside the angle is completely invisible, while a player one degree inside is detected at full strength. Two zones produce a more believable result: a narrow focused cone with strong detection and a wider peripheral cone with reduced intensity.
The peripheral zone should usually build awareness more slowly rather than merely shortening the range. That gives players time to react when they brush the edge of a guard view while preserving fast detection when they cross directly in front of one.
The goal is not physical realism. The goal is a rule the player can learn, predict, and use deliberately.
Aim at the target the player can actually expose
Casting every sight line toward the target transform is fragile because character pivots are often at the feet. Low cover may appear to block the character while the pivot remains visible, or a crouching player may remain detectable through geometry that visually hides them.
Use one or more explicit sample points at meaningful heights. At minimum, let the player controller switch the active sample height when standing or crouching. For more detailed games, sample the head and torso and combine the results. Always start the ray at the guard eye height rather than the guard pivot.
Acquire quickly, lose sight gracefully
Stealth AI often feels unfair when one visible frame causes a full alert, but it also feels unresponsive when every sighting is heavily smoothed. An asymmetric rule works well: accept the first valid sighting immediately, then smooth the loss of sight for a brief period.
That short persistence prevents flickering at railings, door frames, animation poses, and narrow obstacles. Once the persistence window expires, stop feeding live target information and preserve only the last confirmed position. The guard can then investigate what it knows instead of tracking the player through a wall.
Make every vision decision inspectable
A scene view cone is useful, but it is not enough. During play, expose the measured distance, angle, active zone, line-of-sight blocker, lighting multiplier, final detection strength, and estimated time to the next suspicion tier.
When a designer reports that a guard saw through a wall, the answer should be visible in one frame. A sensor that can explain itself is much faster to ship than one that only returns true or false.
- Draw the eye-to-target ray and name the collider that blocked it.
- Show focused and peripheral thresholds separately.
- Display raw evidence before awareness smoothing.
- Warn when masks, ranges, or sample heights are misconfigured.
Argus already connects these pieces.
Argus packages perception, awareness, memory, suspicion, search, squad coordination, and live debugging into a drop-in Unity 6 system. Use the included guard or subscribe to its events and keep your existing movement, animation, combat, and character art.