Course contentsShow
← Unreal Engine Game Development
Lesson 432 of 8806. AI, Navigation, and Decision MakingFree lesson

Implementing Distance and Trace Tests

Learners will apply distance and line-of-sight tests to filter spatial points based on proximity to targets and environmental visibility.

In this lesson, you will learn how to refine your Environmental Query System (EQS) results by filtering spatial points based on their physical distance from an actor and their line-of-sight visibility.

The Logic of Spatial Filtering

When an AI agent searches for a tactical position, the EQS generator might return hundreds of potential points. To make the AI act intelligently, we must discard points that are too far away or blocked by walls. We use Tests within the EQS editor to evaluate these candidates mathematically.

Distance Tests

The Distance Test calculates the magnitude between your generated points and a target (usually the Player or an Objective). You define this in the Test Details panel by setting the Test Purpose to Score Only or Filter.

  • Distance To: Choose DistanceToItem (distance to the generated point) or DistanceToContext (distance to the target actor).
  • Normalization: Distance is usually normalized to a 0-1 range based on your Minimum and Maximum thresholds. Points outside these bounds are culled, while points within are scored—closer points typically receive a higher score if you set the Scoring Equation to Inverse Linear.

Trace Tests (Line-of-Sight)

Even if a point is close, it might be behind a solid pillar. The Trace Test performs a raycast from the AI's "Context" (usually their eyes) to the potential location.

  • Trace Channel: Set this to Visibility or Camera to respect your collision geometry.
  • Test Purpose: Set this to Filter. If the line-of-sight is blocked, the test returns false, effectively removing that point from the AI's list of viable locations.

Implementing in the EQS Editor

  1. Right-click your Query and select Add Test > Distance.
  2. Set the Distance Type to DistanceToContext to evaluate proximity to the player.
  3. Right-click again and add Add Test > Trace.
  4. Ensure Trace Channel is configured correctly to hit walls.
  5. In the Trace test settings, ensure Visibility is checked for Visible or Not Visible depending on whether you want the AI to hide or seek.

Key Takeaway: By combining Distance and Trace tests, you transform raw spatial data into intelligent tactical choices, ensuring your AI only selects spots that are both physically accessible and strategically viable.