- Home /
Finding the nearest reachable point in a particular NavMesh area
Hi there,
I have a NavMesh consisting primarily of a variable height 'land' mesh, and a 'water' plane intersecting it. The land mesh is traversed by NavMeshAgents in search of food and water. I'm trying to figure out how to find the closest reachable point on the water plane to a particular NavMeshAgent.
The water plane underlies the entire map and is only visible in areas where the land mesh is sufficiently low in height. Because of this, functions like Physics.ClosestPoint() are problematic, because the point returned will always be underground (directly below the NavMeshAgent).
I've also considered collecting a sample of positions falling within a certain radius of the NavMeshAgent and using ray casting to determine if there's water at any of the points. I think this should work, but it's not terribly precise, and will require a pretty substantial number of rays as I increase agent count.
See below for a photo of my scene - red slime NavMeshAgent in search of the nearest watering hole, which is roughly at the red X.
Does anyone have any thoughts on this?
Thanks!
Answer by modav6 · Mar 11, 2021 at 07:58 PM
After some further thought, I think I'm going to try a height-based approach:
If the agent is thirsty, select a random position on the X & Z planes within a certain radius of the agent.
If that position happens to be water (determined through raycasting from above), navigate to it using NavMeshAgent.SetDestination(). I have it set so that agents can't traverse 'water' area masks, so I believe this will take the agent right to the edge.
If the position isn't water, but is lower in elevation, navigate to it. The agent should be getting closer to water.
If the position is higher in elevation, find a new position.
This has a few limitations:
If the agent finds itself in a lowland, and it doesn't have sufficient search radius to escape it, it'll end up in an endless loop.
This only works if all water is lower in elevation than all land (no mountain lakes).
I've also learned that many animals satisfy all of their water requirements through the food they eat, dew accumulation, rain, the moisture in the air, etc., and do not require standing bodies of water to drink from. Food for thought if anyone happens to read this far :-).
Your answer
Follow this Question
Related Questions
Can't get NavMeshAgent move. NavMeshAgent ignores destination property 7 Answers
NavMesh Jumping Unity Indie 0 Answers
NavMesh giving jerky like motion 0 Answers
Toggling NavMeshAgent and NavMeshObstacle 2 Answers
check if agent reach destination 1 Answer