AI Perception can't detected actor

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 :open_mouth:

TestActor detail panel :open_mouth:

Hi, by default all pawns will autoregister as sources in the perception system, so if you haven’t disabled that you don’t need any perception stimuli source in the character.

As for why your perception stimuli component doesn’t work, I never really used it, but for me checking “Auto Register as Source” and adding the sight sense in “Register as Source for Senses” works. In your case the “Auto Register as Source” is not checked, and the array of sense you want it to register to is empty.


In your code, at least the “RegisterWithPerceptionSystem” won’t do anything, since the array of senses you want to register to is empty. As for why the RegisterForSense doesn’t work, you could try to call that in the EventBeginPlay, or set breakpoints in the code to see where its not working (and maybe also try to use UAISenseSight::StaticClass() as input).

1 Like

Thank you for your advice!

yees, RegisterForSense and RegisterWithPerceptionSystem
was not worked at Constructor because they call getworld function.

and i didn’t know that Pawn class auto register as a source :open_mouth:

Thank you!!!

  1. Pawn Class don’t need StimuliSourceComponent.
  2. RegisterForSense, RegisterWithPerceptionSystem functions are call GetWorld(),
    so have to call these function at PostInitializeComponents or BeginPlay.
//TestActor.cpp
ATestActor::ATestActor()
{
	StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
	RootComponent = StaticMesh;

	StimuliSourceComp = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("StimuliSourceComp"));
	StimuliSourceComp->bAutoRegister = true;	
}

void ATestActor::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	StimuliSourceComp->RegisterForSense(UAISense_Sight::StaticClass());
	StimuliSourceComp->RegisterWithPerceptionSystem();
	
}


1 Like