What is the most efficient way to find the closest pawn (level has multiple pawns/Actors) ?
You can use PawnIterator , it’s faster that ActorIterator or ObjectIterator.
Here is an example :
float NearestDistance = BIG_NUMBER;
APawn* NearestPawn = nullptr;
for (FConstPawnIterator Iterator = GetWorld()->GetPawnIterator(); Iterator; ++Iterator)
{
APawn* OtherPawn = *Iterator;
if (!IsValid(OtherPawn)) continue;
const float TempDistance = FVector::Dist(GetActorLocation(), OtherPawn->GetActorLocation());
if (NearestDistance > TempDistance)
{
NearestDistance = TempDistance;
NearestPawn = OtherPawn;
}
}
if(NearestPawn)
{
DrawDebugSphere(GetWorld(), NearestPawn->GetActorLocation(), 50.0f, 16, FColor::Red);
}
This is so cool, I just gotta make a small tweak based on the current pawn’s direction but this will do the magic.
Thanks Mhouse1247