In this lesson, you will learn how to make specific environmental objects detectable by AI by attaching and configuring the AIPerceptionStimuliSourceComponent.
Why Environmental Objects Need Stimuli
By default, the AI Perception system primarily tracks other AI characters or the player. However, in complex environments, you often need your AI to recognize hazards (like a ticking bomb) or items (like a health pack) as distinct entities. Instead of forcing the AI to "poll" for these objects, we use the AIPerceptionStimuliSourceComponent to allow objects to "announce" their presence to the AI system whenever they are relevant.
Implementing the Stimuli Source
To make an actor "perceivable," follow these steps:
- Add the Component: Open your environmental actor (e.g., a
BP_EnvironmentalHazard) and add anAIPerceptionStimuliSourcecomponent. - Configure Senses: In the component settings, find the Auto Register as Source option. Ensure it is checked. Under the Register as Source for Senses array, add the specific sense types you want the object to trigger, such as
AISense_SightorAISense_Hearing. - Dynamic Registration (C++): If you need to toggle an object's detectability during runtime (like turning off a light or silencing a hazard), you can manage the registration manually in your Actor’s code:
// Within your Actor class
void AMyEnvironmentalActor::BeginPlay()
{
Super::BeginPlay();
// Register this object as a source for a specific sense
UAIPerceptionSystem::GetCurrent(GetWorld())->RegisterSource(*this, UAISense_Sight::StaticClass());
}
Managing Perception
When you register a component as a source, the AIPerceptionSystem handles the heavy lifting. The AI’s AIPerceptionComponent will automatically detect the actor if it satisfies the sensing requirements. If you have a hazard that only becomes "dangerous" when active, you can call UnregisterSource when the hazard is inactive to remove it from the AI's detection list, effectively making the AI ignore the object until it becomes a threat again.
This approach keeps your AI logic decoupled from your environmental objects, allowing designers to simply drop "Stimuli Source" components onto any asset without writing custom detection logic for every new item.
Key Takeaway: The AIPerceptionStimuliSourceComponent allows environmental actors to broadcast their presence to AI senses, enabling dynamic and scalable detection without hard-coding object types into your AI controllers.