Im trying to get a random position that im sure is on the nav mesh. I use SamplePosition to give me the closes on mesh position on the nav mesh by calling another method that generates a random position that is sure within the screen field.
I try this several times but mostly i get a postion Vector3(infinity,infinity,infinity)
public static Vector3 RandomMapPosition()
{
Vector3 pos = new Vector3(Random.value, Random.value, Camera.main.transform.position.z);
var searchPosition = Camera.main.ViewportToWorldPoint(pos);
return searchPosition;
}
public static Vector3 RandomNavMeshPosition()
{
var randomPos = RandomMapPosition();
NavMeshHit hit;
NavMesh.SamplePosition(RandomMapPosition(), out hit, 1.0f, NavMesh.AllAreas);
return hit.position;
}