AI Perception issue and debug visualization (C++)

Hello there,

I am currently working on a project in Unreal Engine 5 and encountering two issues related to AI perception and debug visualization in my custom AI controller, TowerAIController, which inherits from AAIController.

  1. Inconsistencies with AI Sight Perception: Despite setting up the UAISenseConfig_Sight parameters in my ATowerAIController, the in-game perception appears inconsistent. The perception cone seems to detect actors even when they are not within the specified angles and distances. It looks like I might be misunderstanding something or I did something wrong, but I can’t find where the problem is. Right now it detects everywhere, but when trying different configurations, sometimes it only detects at the tower’s right instead of making sense with the tower’s forward vector and what the cone is supposed to be.

Right now I have the following code:

TowerAIControler.h

Summary
#pragma once

#include "CoreMinimal.h"
#include "AIController.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
#include "Perception/AISenseConfig_Hearing.h"
#include "TowerAIController.generated.h"

/**
 * 
 */
UCLASS()
class TOONTANKS_API ATowerAIController : public AAIController
{
	GENERATED_BODY()
	
public:
	ATowerAIController();

protected:
	virtual void OnPossess(APawn* tower) override;	

	UFUNCTION()
	void OnTargetDetected(AActor* Actor, FAIStimulus Stimulus);

	// AI Perception component
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Perception")
	UAIPerceptionComponent* PerceptionAIComponent;

	// Sight
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Perception")
	UAISenseConfig_Sight* SightAIConfig;

private:
	void ConfigureAISight();
	APawn* _possesedTower;

};

TowerAIController.cpp

Summary
#include "Kismet/KismetSystemLibrary.h"
#include "Tank.h"
#include "Logging/LogMacros.h"
#include "DrawDebugHelpers.h"
#include "TowerAIController.h"

ATowerAIController::ATowerAIController()
{
    PerceptionAIComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));
    ConfigureAISight();
    UE_LOG(LogTemp, Warning, TEXT("ATowerAIController initialized"));    
}

void ATowerAIController::OnPossess(APawn* tower) {
	_possesedTower = tower;
    UE_LOG(LogTemp, Warning, TEXT("ATowerAIController Tower possessed"));
}

void ATowerAIController::ConfigureAISight()
{
    // Settings
    SightAIConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
    SightAIConfig->SightRadius = 1000.0f;
    SightAIConfig->LoseSightRadius = SightAIConfig->SightRadius + 25.0f;
    SightAIConfig->PeripheralVisionAngleDegrees = 180.0f;
    SightAIConfig->SetMaxAge(0.0f);
    SightAIConfig->AutoSuccessRangeFromLastSeenLocation = SightAIConfig->SightRadius;
    SightAIConfig->DetectionByAffiliation.bDetectEnemies = true;
    SightAIConfig->DetectionByAffiliation.bDetectFriendlies = true;
    SightAIConfig->DetectionByAffiliation.bDetectNeutrals = true;

    // Apply configuration
    PerceptionAIComponent->ConfigureSense(*SightAIConfig);
    PerceptionAIComponent->SetDominantSense(*SightAIConfig->GetSenseImplementation());

    PerceptionAIComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ATowerAIController::OnTargetDetected);
}

void ATowerAIController::OnTargetDetected(AActor* actor, FAIStimulus stimulus)
{  

    if (actor->IsA<ATank>())
        UE_LOG(LogTemp, Warning, TEXT("Perception AI: player Tank detected"));


    FVector TowerLocation = _possesedTower->GetActorLocation();
    FVector PlayerLocation = actor->GetActorLocation();
    float DistanceToPlayer = FVector::Dist(TowerLocation, PlayerLocation);

    UE_LOG(LogTemp, Warning, TEXT("Distance to player Tank: %f"), DistanceToPlayer);

}

I have recorded a quick video in order to ilustrate what I mean:

  1. Issue with Debug Visualization of AI Perceptiom: When I assign my custom TowerAIController to the tower in my game, the debug visualization shows “no controller”. However, if I switch back to the default AIController in the Blueprint’s details, the debug visualization indicates a controller is present, but then the AI perception functionalities don’t work as expected.

I have checked the code for errors, ensured all components are initialized correctly, and verified that the TowerAIController is properly assigned in the Unreal Editor. I press the ' and the in the numpad 4 or 5, but I can’t see the perception at all. This can be seen in the following screenshot:

Here, if I change the controller to AIController, it is shown within the game, but no perception at all.

So, my questions are:

  • How can I ensure that the sight config parameters are accurately represented in-game?
  • How can I properly visualize the AI perception debug cone when using a custom AI Controller?

Any advice or insights would be greatly appreciated. Thank you for your time and help!I

Hello again,

I have made some progress, here is a bit of an update:

  1. Peripheral Vision in AI Sight Perception: After further research and testing, I’ve better understood how peripheral vision works. When setting up the UAISenseConfig_Sight, the PeripheralVisionAngleDegrees parameter defines the field of view for the AI. For instance, setting it to 45 degrees means the AI will have a 45-degree angle of vision on each side of its forward direction, totaling a 90-degree field of view.

  2. Issue with AI Perception and Debug Visualization: I discovered the problem was related to how the controller is loaded and assigned via C++. When I created a temporary Blueprint AI controller with a perception component and assigned it to the tower, the system recognized everything correctly, showing the correct controller in debug mode and visualizing the perception lines just fine.

However, when loading and assigning the controller via C++, the editor didn’t seem to detect the controller assigned to the tower properly (it works, but the debug mode doesn’t recognise it).

So, my question now is: How can I ensure that the editor properly recognizes and visualizes de debug lines for an C++ custom AI controller?

Thanks again for any guidance or insights you can provide!

Got it, in the end. I will leave here the solution in case someone has the same problem in the future: If you’re experiencing problems with possessing a APawn using a custom AIController implemented in C++ and associating it through the editor in order to see the debugging AI Perception lines; ensure you call Super::OnPossess within the OnPossess function of your AIController. Here’s the corrected code snippet for my ATowerAIController :

void ATowerAIController::OnPossess(APawn* tower)
{
    Super::OnPossess(tower);  // This
    _possesedTower = tower; 
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.