In this lesson, you will learn how to dynamically calculate an AI’s detection range by applying multipliers based on the player’s current environmental state.
In our previous lessons, we established a static Line of Sight (LoS) using FOV cones. However, a static range is rarely fun. To create compelling stealth, the AI’s "awareness" should be a fluid value calculated by combining the base detection distance with environmental modifiers like light levels, movement speed, and posture.
The Visibility Multiplier Logic
Think of the detection range as a base value (e.g., 10 units) that is multiplied by a visibilityModifier. If the player is crouching, we might multiply the range by 0.5. If the player is standing in a brightly lit area, we might multiply it by 1.5.
We store these modifiers in the Blackboard system, allowing our Behavior Tree nodes to calculate the final "Detection Radius" every frame before the sensory logic executes:
public float CalculateDetectionRange(float baseRange)
{
float lightModifier = environmentSensor.CurrentLightLevel; // 0.0 to 1.0
float postureModifier = playerController.IsCrouching ? 0.5f : 1.0f;
float movementModifier = playerController.IsSprinting ? 1.5f : 1.0f;
return baseRange * lightModifier * postureModifier * movementModifier;
}
Implementing in the Behavior Tree
To keep this modular, we use a Service Node attached to the AI's "Patrol" or "Search" behavior. This service updates the Blackboard with the calculatedRange variable. The "Detect Player" Leaf Node then references this value instead of a hardcoded constant. By doing this, the AI effectively "sees" less effectively when the player is hiding in shadows or moving slowly.
This creates a reactive feedback loop: the player can observe the AI’s behavior, realize that crouching prevents detection, and actively manipulate the environment to bypass challenges. By adjusting these modifiers, you can tune the "difficulty" of your stealth gameplay without ever changing the core AI navigation or state machine logic.
Key Takeaway: Detection range should not be a constant value, but a dynamic calculation that treats environmental factors and player input as multipliers to the AI’s sensory threshold.