Setting Up Detection by Affiliation for AI in Unreal C++
During AI perception setup, I encountered an issue where when two AI characters appear in the scene, they detect eachother and trigger pursuit. To differentiate the detected actors, this functionality is required.
Unreal Official Documentation: This property can be used to set up team-based visual perception. Currently, Affiliation can only be defined in C++. For Blueprints, you can use the Detect Neutrals option to detect all actors, then filter actor types using Tags.
Step 1: Create a header file for the TeamAffiliation enum
// TeamType.h
#pragma once
UENUM(BlueprintType)
enum class ETeamAffiliation : uint8
{
FRIENDLY,
ENEMY,
NEUTRAL
};
Note: Ensure the header file is placed in the correct project path (it may not default to the same location as other code files).
Define the affiliation in the player and AI character classes
// PlayerCharacter.h
#include "TeamType.h"
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Affiliation", meta = (AllowPrivateAccess = "true"))
ETeamAffiliation Affiliation = ETeamAffiliation::FRIENDLY; // Set to friendly
// AICharacter.h
#include "TeamType.h"
#include "AIController.h" // Include the appropriate AI controller
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Affiliation", meta = (AllowPrivateAccess = "true"))
ETeamAffiliation Affiliation = ETeamAffiliation::ENEMY; // Set to enemy
UFUNCTION()
void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus);
// AICharacter.cpp
// In BeginPlay:
EnemyAIController->PerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &AAICharacter::OnTargetPerceptionUpdated);
// Implementation of the perception callback
void AAICharacter::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
if (!Actor)
{
return;
}
if (Stimulus.IsActive()) // When an actor is detected
{
// Check if the detected actor is the player character
APlayerCharacter* Player = Cast<APlayerCharacter>(Actor);
// If the affiliation differs, start chasing
if (Player && Player->Affiliation != this->Affiliation)
{
UE_LOG(LogTemp, Warning, TEXT("Controller found player"));
EnemyAIController->GetBlackboardComponent()->SetValueAsObject(TEXT("Target"), Player);
EnemyAIController->GetBlackboardComponent()->SetValueAsBool(TEXT("FindPlayer"), true);
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No player found"));
EnemyAIController->GetBlackboardComponent()->SetValueAsObject(TEXT("Target"), nullptr);
EnemyAIController->GetBlackboardComponent()->SetValueAsBool(TEXT("FindPlayer"), false);
}
}
After this setup, AI characters should no longer chase each other.