- Home /
How to get a random point on NavMesh?
Hi to all,
In my current project i use unity navmesh for ai navigation.i want my npc's be able to get a random walkable destination(point) on navmesh and flee to that point when he/she see the player. Any help or tips on how to do it, I'd be grateful.
Answer by Valkyr_x · Jun 17, 2013 at 04:17 AM
This might not be the best way to do it but it worked for my purpose.
I generated a random point with in a unit sphere and multiplied it by the max distance I wanted my agents to walk:
Vector3 randomDirection = Random.insideUnitSphere * walkRadius;
I then added my agents current position to this vector and used the NavMesh.SamplePosition function to find the close position on the NavMesh:
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1);
Vector3 finalPosition = hit.position;
I then used final postion as the destination for my agent.
Hope this helps!
It works perfectly and my agents can move freely in scene,of course i modified it a few to fit with my script. thanks a lot.
sorry to bump an old thread but what functions do you suggest i use theese in
Hello guys,maybe im too late,but i just want to say,im soo lost,how can i use this script,please put full script?!?!? I dont understand those lines,please put full script with functions and everything,these lines just makes my head hurt. i need to ai flee away from me,but these lines are soo incomprehensible ,PLEASE just finish this f script. Thanks :)
@$$anonymous$$arks981 what don't you understand? If there's any specific point you're not sure about, read the docs about it. If that didn't help, try reading what others say about it. If that didn't help, feel free to ask about it :)
Hello bigbat,
I just wrote the code, hope it will help.
InvokeRepeating("GameObject_ChangePosition", 0.0f, 1.0f);
void GameObject__ChangePosition()
{
GameObject_targetPoint = new Vector3(Random.Range(-8.0F, 8.0F), 0, Random.Range(-4.5F, 4.5F));
}
void Update ()
{
` if(GameObject.GetComponent<Nav$$anonymous$$eshAgent>() != null)
GameObject.SetDestination(GameObject_targetPoint); `}
Answer by Selzier · Oct 27, 2017 at 04:56 PM
Thanks for the answer Valkyr_x! Expanding on that here's a C# function for getting a random Vector3 Navmesh Position:
public Vector3 RandomNavmeshLocation(float radius) {
Vector3 randomDirection = Random.insideUnitSphere * radius;
randomDirection += transform.position;
NavMeshHit hit;
Vector3 finalPosition = Vector3.zero;
if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1)) {
finalPosition = hit.position;
}
return finalPosition;
}
And this can make the NavAgent move by calling this code, with whatever radius you want:
myNavAgent.SetDestination(RandomNavmeshLocation(4f));
can you please explain what the 4f is for? is it the radius or is it time?
It's the "radius" argument in the first line:
public Vector3 RandomNavmeshLocation(float radius)
Beware that this may make an agent that's near the edge of the navmesh run to the origin! (If radius is > 1 and it picks a point more than 1 away from the navmesh, it returns Vector3.zero.)
Better to return transform.position or a Vector3?
so the caller knows when it failed (they'd probably want to retry).