In this lesson, you will learn how to fine-tune your UAIPerceptionComponent to go beyond simple line-of-sight checks by implementing peripheral vision and distance-based visibility thresholds.
Configuring the Sight Config
While basic line-of-sight detects if an actor is "visible," real-world perception requires nuance. We achieve this by configuring the FAISenseConfig_Sight object within your AI Controller’s constructor.
To simulate peripheral vision, we manipulate the PeripheralVisionAngleDegrees property. This value defines the "cone" of vision in front of the AI. If a player is within this angle, the AI considers them "in sight," provided the line-of-sight trace succeeds.
Implementing Distance and Angle Thresholds
You can control how "aware" your AI is at different ranges by adjusting three primary properties:
- SightRadius: The maximum distance the AI can see.
- LoseSightRadius: The distance at which the AI "forgets" the target if they move away. Setting this slightly higher than the
SightRadiusprevents the AI from flickering between states when a player dances on the edge of the detection boundary. - PeripheralVisionAngleDegrees: The half-angle of the vision cone. A value of 90 creates a 180-degree front-facing field of view.
Here is how you configure these in C++:
// Within your AI Controller constructor
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
SightConfig->SightRadius = 3000.0f;
SightConfig->LoseSightRadius = 3500.0f;
SightConfig->PeripheralVisionAngleDegrees = 90.0f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = false;
GetPerceptionComponent()->ConfigureSense(*SightConfig);
GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
Why Distance Matters
By leveraging the LoseSightRadius, you create a hysteresis effect. This is essential for gameplay feel; it ensures that once an AI locks onto a target, the player must actually retreat a significant distance before the AI loses interest. This prevents the "jitter" where an AI constantly loses and regains perception due to micro-movements near the edge of the SightRadius.
Key Takeaway: Precise control over your AI's SightRadius and PeripheralVisionAngleDegrees allows you to create believable detection behaviors that prioritize focus in front of the agent while managing visual awareness thresholds.