AI Perception (C++) Not Firing

I have already read the Quick AI Perception Guide and surfed through all the tutorials regarding how to set up the AI Perception in C++. Nothing seems to work.

I have managed to only make it work on Blueprint but NOT in C++.

Below is my code:

BaseCharacter.h // Custom Class

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Senses)
class UAIPerceptionComponent* PerceptionComponent;
    
UPROPERTY(BlueprintReadWrite, Category = Senses)
class UAISenseConfig_Sight* SightConfig;

UFUNCTION()
void UpdatePerception(TArray<AActor*>ActorsInSight);

BaseCharacter.cpp

ABaseCharacter::ABaseCharacter()
{
    // ....
    PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AI Perception Component"));
    SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("AI Sight Config"));
    PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &ABaseCharacter::UpdatePerception);

    SightConfig->SightRadius = 2000.f;
    SightConfig->LoseSightRadius = 2000.f + 20.f;
    SightConfig->PeripheralVisionAngleDegrees = 90.0f;

    SightConfig->DetectionByAffiliation.bDetectEnemies = true;
    SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
    SightConfig->DetectionByAffiliation.bDetectNeutrals = true;

    PerceptionComponent->ConfigureSense(*SightConfig);
    PerceptionComponent->SetDominantSense(SightConfig->GetSenseImplementation());
}

void ABaseCharacter::UpdatePerception(TArray<AActor*>ActorsInSight)
{
    GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("UPDATE PERCEPTION"));
}

I have fixed the problem.

Hi, could you please tell how did you fixed it? I am having exactly the same problem.

Its like because pivot point is below surface.

What did you do to fix this?

U have first SetPerceptionComponent inside the AI Controller class, as there is a default PerceptionComponent within AI Controller. Then, use GetAIPerceptionComponent()->OnPerceptionUpdated.AddDynamic( paramaters ) instead of the AIPerceptionComponent that u constructed by urself. I stucked at this for hours, and finally figure that out by accident.

Hope this message is useful.
Cheers.
UE 4.16

1 Like