# AI Perception can't detected actor

**URL:** <https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989>\
**Category:** C++\
**Tags:** unreal-engine, UE4, CPP, UE4-27, question\
**Created:** [May 14, 2022, 11:31am UTC](https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989 "2022-05-14T11:31:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CupofCoffee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CupofCoffee](https://forums.unrealengine.com/u/CupofCoffee)\
**Post date:** [May 14, 2022, 11:31am UTC](https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989/1 "2022-05-14T11:31:01Z")

</div>

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

```auto
//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

```auto
//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

```auto
//AITestCharacter.h
class AAITestCharacter : public ACharacter
{
	GENERATED_BODY()
       //...
	UPROPERTY(EditDefaultsOnly, Category = AI)
	class UAIPerceptionStimuliSourceComponent* StimuliSourceComp;
       //...
};

```

AITestCharacter .cpp file

```auto
//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

```auto
//TestActor.h
class AITEST_API ATestActor : public AActor
{
	GENERATED_BODY()
	
public:	
	ATestActor();

	UPROPERTY(EditDefaultsOnly)
	UStaticMeshComponent* StaticMesh;
	
	UPROPERTY(EditDefaultsOnly, Category = AI)
	UAIPerceptionStimuliSourceComponent* StimuliSourceComp;
     //...
};

```

```auto
//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

 ![AISightIssue](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/b/ccb1df8dff2515cd7f273d06f6e679175045122e.jpeg)

Character Detail panel 😮

 ![char1 detail](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/3/f037ac821fddde5854c5fe3874c82af13fa78d8d.png)

TestActor detail panel 😮

 ![testactor detail](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/3/d/83da493b3e8e537cd7204439c5eb8c9b49c84ba5.png)

---

<div class="post-metadata">

**Author:** ![chrudimer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chrudimer/32/918651_2.png) [@chrudimer](https://forums.unrealengine.com/u/chrudimer)\
**Post date:** [May 14, 2022, 2:03pm UTC](https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989/2 "2022-05-14T14:03:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![CupofCoffee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CupofCoffee](https://forums.unrealengine.com/u/CupofCoffee)\
**Post date:** [May 14, 2022, 5:02pm UTC](https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989/3 "2022-05-14T17:02:37Z")

</div>

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 😮

Thank you!!!

---

<div class="post-metadata">

**Author:** ![CupofCoffee](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CupofCoffee](https://forums.unrealengine.com/u/CupofCoffee)\
**Post date:** [May 14, 2022, 5:14pm UTC](https://forums.unrealengine.com/t/ai-perception-cant-detected-actor/556989/4 "2022-05-14T17:14:51Z")

</div>

1. Pawn Class don’t need StimuliSourceComponent.
2. RegisterForSense, RegisterWithPerceptionSystem functions are call GetWorld(),  
so have to call these function at PostInitializeComponents or BeginPlay.

```auto
//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();
	
}

```

 ![fixSight](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/9/5/395ed6f2c9d457244b6fa8a7b5c0fedb54b1d583.jpeg)

 ![char1 detail fix](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/f/2/cf2f184c78f26369b7d0bfe1e439c35fa01fb89b.png)  
 ![testactor detail fix](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/c/d/fcdc9dc5624d11383b56a06202a4d07d05e4b170.png)
