/// <summary> /// Returns the nearest destination from the agent. /// </summary> public static bool NearestDestination(this NavMeshAgent agent, Vector3[] destinations, out Vector3 destination, out int destinationIndex) { destination = new Vector3(); destinationIndex = -1; float shortestDistance = float.MaxValue; bool pathFound = false; for (int i = 0; i < destinations.Length; i++) { Vector3 position = destinations[i]; if (agent.DistanceToDestination(position, out float calculatedDistance)) { if (calculatedDistance < shortestDistance) { pathFound = true; shortestDistance = calculatedDistance; destination = position; destinationIndex = i; } } } return(pathFound); }