Skip to content

Instantly share code, notes, and snippets.

@StewMcc
Created April 25, 2018 17:15
Show Gist options
  • Save StewMcc/66c211a88f421e117288a8711adb12c4 to your computer and use it in GitHub Desktop.
Save StewMcc/66c211a88f421e117288a8711adb12c4 to your computer and use it in GitHub Desktop.
public class NavMeshExtensions {
/// <summary>
/// Generates a random point on the navmesh within the radius around the centre.
/// </summary>
/// <param name="center"> Centre point to create sphere around. </param>
/// <param name="radius"> Radius of Sphere. </param>
/// <param name="result"> Resultant NavMesh Point found. Vector3.zero if not found. </param>
/// <param name="maxAttempts"> Maximum number of times to try and find a point default value 30. </param>
/// <returns> Whether or not a navmesh point was found. If false result is Vector3.zero (bad idea to use this) </returns>
public static bool RandomPointOnNavMesh(Vector3 center, float radius, out Vector3 result, int maxAttempts = 30) {
for (int i = 0; i < maxAttempts; i++) {
Vector3 randomPoint = center + Random.insideUnitSphere * radius;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) {
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment