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

Configuring AI Perception Component for Sensory Input

Learn to attach and initialize the AI Perception component to your character to enable modular sensory data processing.

In this lesson, you’ll learn how to attach and configure the UAIPerceptionComponent in C++ to enable your AI to process modular sensory data like sight, hearing, and damage.

The Modular Approach

Instead of hardcoding collision checks or distance calculations, we use the UAIPerceptionComponent. It acts as a central hub that registers "Senses"—modular objects that handle the math of detecting actors. By separating the perception logic from the AI's controller, we can hot-swap detection behaviors without refactoring our tactical state machines.

Implementation

First, define and initialize the component in your AAIController subclass. You must include Perception/AIPerceptionComponent.h and Perception/AISenseConfig_Sight.h in your header.

In your constructor, create the component and register the senses:

// Constructor
MyAIController::MyAIController() {
    PerceptionComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComp"));
    SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
    
    // Configure Sight settings
    SightConfig->SightRadius = 2000.0f;
    SightConfig->LoseSightRadius = 2500.0f;
    SightConfig->DetectionByAffiliation.bDetectEnemies = true;
    
    // Attach to component
    PerceptionComp->ConfigureSense(*SightConfig);
    PerceptionComp->SetDominantSense(*SightConfig->GetSenseImplementation());
}

Binding to Events

Once configured, you don’t manually poll for actors. Instead, you bind a callback function to the OnTargetPerceptionUpdated delegate. This ensures your AI only processes data when a sense state changes (e.g., an enemy enters the vision cone), which is significantly more efficient than running trace tests every tick.

PerceptionComp->OnTargetPerceptionUpdated.AddDynamic(this, &MyAIController::OnTargetDetected);

Within OnTargetDetected, you receive an AActor* and an FAIStimulus struct. This stimulus contains the data you need to update your Blackboard keys, which your Behavior Tree then uses to trigger the high-level decision-making logic we’ve built in previous lessons. By funneling sensory input into the Blackboard via this component, you maintain a clean separation between raw environmental data and tactical behavior.

Key Takeaway: Use the AI Perception Component to centralize sensory processing, allowing your AI to react to the world through event-driven callbacks rather than resource-heavy polling.