In this lesson, you will learn how to evolve your AI’s situational awareness by migrating from basic PawnSensing to the robust, modular AIPerception system.
Moving Beyond Basic Sensing
While PawnSensing is great for simple "line of sight" or "noise" events, it lacks the flexibility needed for complex behaviors. The AIPerception component acts as a central hub where you can register multiple "senses," such as AISense_Sight, AISense_Hearing, and AISense_Damage. Instead of the AI checking for stimuli every tick, the perception system broadcasts events only when a sensory threshold is met, significantly optimizing performance.
Implementing Multi-Sensory Data
To utilize this system, you add the AIPerception component to your AIController and configure the senses in the Senses Config array. Unlike basic sensing, AIPerception provides a PerceptionUpdated delegate. When an actor enters a range or makes a noise, the controller receives an Actor reference and an FAIStimulus struct containing the specific data (e.g., location, tags, and whether the stimulus was successfully registered).
For example, when handling damage, you can distinguish between different sources:
// In your AI Controller
void AMyAIController::HandlePerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
if (Stimulus.Type == UAISense::GetSenseID<UAISense_Damage>())
{
// React to the specific location of the damage source
float DamageAmount = Stimulus.Strength;
FVector DamageLocation = Stimulus.StimulusLocation;
// Logic to face the threat or flee
}
}
Why This Matters
By using AIPerception, your AI can now filter data. You can set up "Sight" to only register specific teams or actors using AIPerceptionComponent filter settings. Because this system is event-driven, you no longer need to write custom "Can I see him?" timers in your Tick function. This modular approach allows you to plug in new senses, like AISense_Prediction or AISense_Team, without re-architecting your entire AI sensing logic.
Key Takeaway: Replace basic PawnSensing with the modular AIPerception system to handle event-driven, multi-channel sensory inputs that are more performant and easier to filter for complex AI behaviors.