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

Processing Damage Events for Reactive Combat AI

Implement the damage perception sense to allow AI to instantly identify and face the source of incoming attacks.

In this lesson, you will learn how to leverage the AI Perception component’s Damage Sense to force your AI to instantly detect and rotate toward the source of incoming attacks.

Why Damage Perception?

While Vision and Hearing are passive senses, the Damage Sense is an active event-driven system. When an AI receives damage, we don't want to wait for the next EQS query or tick-based update to find the enemy. We want the AI to "snap" to the threat, allowing it to transition immediately into a combat state.

Implementing the Damage Sense

To achieve this, ensure your UAIPerceptionComponent has the Damage Sense enabled. Unlike vision, you don’t configure this via a config asset in the same way; instead, you trigger it programmatically using ReportDamageEvent.

When your AI character takes damage (e.g., inside TakeDamage), you notify the perception system of the attacker:

void AMyAICharacter::TakeDamage(float DamageAmount, ...) 
{
    Super::TakeDamage(...);

    // Report the event to the perception system
    UAIPerceptionSystem::GetCurrent(GetWorld())->ReportEvent(
        FAIDamageEvent(this, Instigator, DamageAmount, GetActorLocation(), HitLocation)
    );
}

Reacting to the Stimulus

Once the event is reported, your AI’s OnPerceptionUpdated (or OnTargetPerceptionUpdated) delegate will fire. Within this logic, you must update your Blackboard keys to reflect the new "TargetActor."

To make the AI face the attacker instantly, you can use the AAIController::SetFocus method inside your perception logic:

void AMyAIController::OnPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
    if (Stimulus.Type == UDamageSenseConfig::GetSenseID())
    {
        // Update Blackboard for the Behavior Tree to process
        Blackboard->SetValueAsObject("TargetActor", Actor);
        
        // Force the AI to rotate toward the attacker immediately
        SetFocus(Actor);
    }
}

By coupling the FAIDamageEvent with SetFocus, you bypass the delay of standard tree-based targeting, creating a AI that feels hyper-aware and reactive during heated combat exchanges.

Key Takeaway: Use the Damage Sense to trigger immediate event-based focus, ensuring your AI responds to physical threats faster than standard sensory polling allows.