Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Created August 20, 2018 14:08
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save IJEMIN/f2510a85b1aaf3517da1af7a6f9f1ed3 to your computer and use it in GitHub Desktop.
Save IJEMIN/f2510a85b1aaf3517da1af7a6f9f1ed3 to your computer and use it in GitHub Desktop.
Get Random Position on NavMesh in Unity
using UnityEngine.AI;
using UnityEngine;
public static class NavMeshUtil {
// Get Random Point on a Navmesh surface
public static Vector3 GetRandomPoint(Vector3 center, float maxDistance) {
// Get Random Point inside Sphere which position is center, radius is maxDistance
Vector3 randomPos = Random.insideUnitSphere * maxDistance + center;
NavMeshHit hit; // NavMesh Sampling Info Container
// from randomPos find a nearest point on NavMesh surface in range of maxDistance
NavMesh.SamplePosition(randomPos, out hit, maxDistance, NavMesh.AllAreas);
return hit.position;
}
}
@localhosted
Copy link

If you don't mind having the "random" points in a grid, you can pregenerate points according to the navmesh's bounds and generate points using Mathf.PerlinNoise

Afterwards you can exclude all points that aren't found by NavMesh.SamplePosition and then choose a random valid point with Random.Range(0, pointList.Count)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment