Hello,
I was wondering if there is a way to get a random Vector3 point from a Navmesh. Thanks.
There should be an easy way to do this (for when I would like my characters to walk along random points within a Navmesh area). I already know the best approach for this is to probably create code that would pick a random Vector3 point within a 3D rectangular area and have the Navmesh agent walk along only the random walkable nodes if the random point that the computer picks falls on a walkable node, but what if I want a script to automatically detect the areas of all the Navmeshes instead of me having to manually create rectangular areas that the random point should fall on, and include only the walkable nodes and have the computer only pick random points that are walkable instead of creating a time-consuming loop that keeps looping until a walkable point is chosen?.. and since Navmeshes can be any shape, I would like to work with Navmeshes instead of multiple rectangles. Is there a way to do this?
Well there are two ways to do it and I’ve been looking into both.
agent.SetDestination (Random.insideUnitSphere * 350);
the “agent” name being a NavMeshAgent you have grabbed from the game world that has the script attached, this will allow the AI to wonder within a certain radius on a navigation mesh which you’ll need to set up either way.
If however you’re looking for waypoints what I would do is use SetDestination and have empties or whatever GameObject you’re using as a waypoint to but unfortunately I have just run into this problem because my agents don’t go to random points like you want they go straight to the first GameObject I have placed in the game world.
Edit: Damn sorry, I misunderstood what you were asking, I think what you’re asking is beyond me and possible to do with a grid system.
Although, if you tweak your Navigation Mesh, it’s entirely possible you could use InsideUnitSphere and some empty colliders to get the desired effect, if colliders are there on the navmesh then in theory the agents should just avoid it no problem even though there’s nothing there for the player to see.
They should have a way to get the current NavMesh object that the agent is standing on, like…
public class NavMeshAgent {
...
public NavMesh getNavMesh() {
...
}
...
}
agent.getNavMesh();
… and then a way to get a random point off that NavMesh, something like…
public class NavMesh {
...
public Vector3 getRandomPoint() {
...
}
...
}
Vector3 destination = agent.getNavMesh().getRandomPoint();
agent.SetDestination(destination);
Wouldn’t this be a nice feature to add?
unnecessary. You can achieve the functionality without relying on a built in helper.
Thank you.