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
.
- Inconsistencies with AI Sight Perception: Despite setting up the
UAISenseConfig_Sight
parameters in myATowerAIController
, 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:
- 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 defaultAIController
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