Update Enemy AI Controller for Increased Sight Perception

This commit is contained in:
Philip W 2023-10-02 01:31:06 +01:00
parent b718569448
commit 174becfdf1

View File

@ -5,6 +5,7 @@
#include "EnemyCharacter.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "EndlessVendetta/EndlessVendettaCharacter.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
@ -43,6 +44,10 @@ void AAI_EnemyController::OnPossess(APawn* InPawn)
void AAI_EnemyController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (GetBlackboardComponent()->GetValueAsBool("CanSeePlayer"))
{
GetBlackboardComponent()->SetValueAsVector("TargetLocation", GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation());
}
}
void AAI_EnemyController::SetupPerceptionSystem()
@ -51,10 +56,10 @@ void AAI_EnemyController::SetupPerceptionSystem()
if (IsValid(SightConfig))
{
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
SightConfig->SightRadius = 1000.0f;
SightConfig->LoseSightRadius = 1100.0f;
SightConfig->PeripheralVisionAngleDegrees = 90.0f;
SightConfig->SetMaxAge(5.0f);
SightConfig->SightRadius = 2000.0f;
SightConfig->LoseSightRadius = 2100.0f;
SightConfig->PeripheralVisionAngleDegrees = 70.0f;
SightConfig->SetMaxAge(20.0f);
SightConfig->AutoSuccessRangeFromLastSeenLocation = 520.0f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
@ -67,17 +72,19 @@ void AAI_EnemyController::SetupPerceptionSystem()
void AAI_EnemyController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus)
{
if (AEnemyCharacter* const EnemyCharacter = Cast<AEnemyCharacter>(Actor))
if (AEndlessVendettaCharacter* const PlayerCharacter = Cast<AEndlessVendettaCharacter>(Actor))
{
if (Stimulus.WasSuccessfullySensed())
{
GetBlackboardComponent()->SetValueAsObject("TargetActor", Actor);
GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor);
GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation);
GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true);
}
else
{
GetBlackboardComponent()->ClearValue("TargetActor");
GetBlackboardComponent()->ClearValue("TargetLocation");
GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false);
}
}
}