Can someone tell me what is going wrong here? It’s a “closest enemy” code.
I want it to only use the enemies with “attackable” tag. Upon doing a test. It’s giving me
all sorts of weird results. It finds the enemy but doesn’t necessarily give me the closest. Even flat out ignoring enemies right up close.
float Distanceval = 10000.f;
FVector myLocation = GetActorLocation();
TArray<AActor*> EnemiesInSphereRadius;
TSubclassOf < APlayerGeo > ClassFilter;
AActor* ClosestActor = nullptr;// Clear the array
EnemiesInSphereRadius.Empty();if (EnemiesInSphereRadius.Num() > 0)
{
EnemiesInSphereRadius.RemoveAt(0);
}// Generate/populate the “EnemiesInSphereRadius” array with the resulting actors that are overlapped
HominCone->GetOverlappingActors(EnemiesInSphereRadius, ClassFilter);
if (EnemiesInSphereRadius.Num() > 0)
{
// Set the initial distance float
float InitialDistance1 = FMath::RoundToPositiveInfinity(900000000.0f);for (int32 ActorIndex = 0; ActorIndex < EnemiesInSphereRadius.Num(); ActorIndex++) { float ClosestDistance1 = FVector(myLocation - EnemiesInSphereRadius[ActorIndex]->GetActorLocation()).SquaredLength();//SizeSquared(); //.Size(); if (!ActorIndex || !EnemiesInSphereRadius[ActorIndex]->ActorHasTag(TEXT("Attackable"))) { continue; } // Get the closest actor by distance to the main character in the array. if (ClosestDistance1 < InitialDistance1) { InitialDistance1 = ClosestDistance1; ClosestActor = EnemiesInSphereRadius[ActorIndex]; } }
}