I don’t know why my enemy won’t detect the player, all the enemies are always trying to go outside the map, but won’t detect the player, please give me a solution been trying to solve it for weeks now. NOTE - I have multiple enemies around 60 or something maybe that could be the problem
Enemy script:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Enemy.h"
#include "Components/BoxComponent.h"
#include "MonsterShooterCharacter.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
// Sets default values
AEnemy::AEnemy()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
DamageCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("Damage Collision"));
DamageCollision->SetupAttachment(RootComponent);
AIPerComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AI Perception Component"));
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
SightConfig->SightRadius = 10000.0f;
SightConfig->LoseSightRadius = 10030.0f;
SightConfig->PeripheralVisionAngleDegrees = 90.0f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
SightConfig->SetMaxAge(0.1f);
AIPerComp->ConfigureSense(*SightConfig);
AIPerComp->SetDominantSense(SightConfig->GetSenseImplementation());
AIPerComp->OnPerceptionUpdated.AddDynamic(this, &AEnemy::OnSensed);
CurrentVelocity = FVector::ZeroVector;
MovementSpeed = -375.0f;
DistanceSquared = BIG_NUMBER;
}
// Called when the game starts or when spawned
void AEnemy::BeginPlay()
{
Super::BeginPlay();
DamageCollision->OnComponentBeginOverlap.AddDynamic(this, &AEnemy::OnHit);
BaseLocation = this->GetActorLocation();
}
// Called every frame
void AEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!CurrentVelocity.IsZero())
{
NewLocation = GetActorLocation() + CurrentVelocity * DeltaTime;
if (BackToBaseLocation)
{
if ((NewLocation - BaseLocation).SizeSquared2D() < DistanceSquared)
{
DistanceSquared = (NewLocation - BaseLocation).SizeSquared2D();
}
else
{
CurrentVelocity = FVector::ZeroVector;
DistanceSquared = BIG_NUMBER;
BackToBaseLocation = false;
SetNewRotation(GetActorForwardVector(), GetActorLocation());
}
}
SetActorLocation(NewLocation);
}
}
// Called to bind functionality to input
void AEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AEnemy::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& Hit)
{
AMonsterShooterCharacter* Char = Cast<AMonsterShooterCharacter>(OtherActor);
if (Char)
{
Char->DealDamage(DamageValue);
}
}
void AEnemy::OnSensed(const TArray& UpdatedActors)
{
for (int i = 0; i < UpdatedActors.Num(); i++)
{
FActorPerceptionBlueprintInfo Info;
AIPerComp->GetActorsPerception(UpdatedActors[i], Info);
if (Info.LastSensedStimuli[0].WasSuccessfullySensed())
{
FVector dir = UpdatedActors[i]->GetActorLocation() - GetActorLocation();
dir.Z = 0.0f;
CurrentVelocity = dir.GetSafeNormal() * MovementSpeed;
SetNewRotation(UpdatedActors[i]->GetActorLocation(), GetActorLocation());
}
else
{
FVector dir = BaseLocation - GetActorLocation();
dir.Z = 0.0f;
if (dir.SizeSquared2D() > 1.0f)
{
CurrentVelocity = dir.GetSafeNormal() * MovementSpeed;
BackToBaseLocation = true;
SetNewRotation(BaseLocation, GetActorLocation());
}
}
}
}
void AEnemy::SetNewRotation(FVector TargetPosition, FVector CurrentPosition)
{
FVector NewDirection = TargetPosition - CurrentPosition;
NewDirection.Z = 0.0f;
EnemyRotation = NewDirection.Rotation();
SetActorRotation(EnemyRotation);
}
void AEnemy::DealDamage(float DamageAmount)
{
Health -= DamageAmount;
if (Health <= 0.0f)
{
Destroy();
}
}