ASBaseCharacter* ASZombieCharacter::GetClosestPlayer()
{
float ClosestDistance = 10000.0f;
ASBaseCharacter* ClosestPlayer;
//Get all PlayerControllers in the multiplayer gameworld
for (TActorIterator<APlayerController> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
if (!ActorItr) return; //will this line break out of function if no PlayerController found?
//For each of the found player characters get the ASBaseCharacter
ASBaseCharacter* Player = Cast<ASBaseCharacter>(ActorItr->GetPawn());
//get the distance (float) from the AI to the Player Pawn position
FVector difference = Player->GetActorLocation() - GetActorLocation();
float distance = difference.Size();
if (distance < ClosestDistance)
{
ClosestDistance = distance;
ClosestPlayer = Player;
}
}
return ClosestPlayer;
}