Hello
I want AI Character can recognize several different actor.
But AI Perception detected only Character class.
i created small test project to ask a question.
CustomAIController Header
//CustomAIController.h
class UAIPerceptionComponent;
class UAISenseConfig_Sight;
UCLASS()
class AITEST_API ACustomAIController : public AAIController
{
GENERATED_BODY()
public:
ACustomAIController();
UPROPERTY(EditDefaultsOnly, Category = AI)
UAISenseConfig_Sight* SightSense;
virtual void PostInitializeComponents() override;
UFUNCTION()
void TakeSense(AActor* Actor, FAIStimulus Stimulus);
};
CustomAIController cpp file
//CustomAIController.cpp
ACustomAIController::ACustomAIController()
{
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComp")));
SightSense = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightSense"));
SightSense->SightRadius = 1000.f;
SightSense->LoseSightRadius = 1500.f;
SightSense->PeripheralVisionAngleDegrees = 60.f;
SightSense->DetectionByAffiliation.bDetectEnemies = true;
SightSense->DetectionByAffiliation.bDetectFriendlies = true;
SightSense->DetectionByAffiliation.bDetectNeutrals = true;
GetPerceptionComponent()->ConfigureSense(*SightSense);
GetPerceptionComponent()->SetDominantSense(*SightSense->GetSenseImplementation());
}
void ACustomAIController::PostInitializeComponents()
{
Super::PostInitializeComponents();
GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &ACustomAIController::TakeSense);
}
void ACustomAIController::TakeSense(AActor* Actor, FAIStimulus Stimulus)
{
UE_LOG(LogTemp, Warning, TEXT("OnTargetPerception Updated function "));
}
and AITestCharacter header file
//AITestCharacter.h
class AAITestCharacter : public ACharacter
{
GENERATED_BODY()
//...
UPROPERTY(EditDefaultsOnly, Category = AI)
class UAIPerceptionStimuliSourceComponent* StimuliSourceComp;
//...
};
AITestCharacter .cpp file
//AITestCharacter.cpp
AAITestCharacter::AAITestCharacter()
{
StimuliSourceComp = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("StimuliSourceComp"));
StimuliSourceComp->RegisterForSense(TSubclassOf<UAISense_Sight>());
StimuliSourceComp->bAutoRegister = true;
StimuliSourceComp->RegisterWithPerceptionSystem();
}
finally TestActor header file And cpp file
//TestActor.h
class AITEST_API ATestActor : public AActor
{
GENERATED_BODY()
public:
ATestActor();
UPROPERTY(EditDefaultsOnly)
UStaticMeshComponent* StaticMesh;
UPROPERTY(EditDefaultsOnly, Category = AI)
UAIPerceptionStimuliSourceComponent* StimuliSourceComp;
//...
};
//TestActor.cpp
ATestActor::ATestActor()
{
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
RootComponent = StaticMesh;
StimuliSourceComp = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("StimuliSourceComp"));
StimuliSourceComp->bAutoRegister = true;
StimuliSourceComp->RegisterForSense(TSubclassOf<UAISense_Sight>());
StimuliSourceComp->RegisterWithPerceptionSystem();
}
Ai can’t detect TestActor
Character Detail panel
TestActor detail panel