0
\$\begingroup\$

I'm fairly new to unity.I've been playing with Nav Mesh Agents lately.I have encountered this problem twice.The first time I didn't bother redoing my project all over again.But now its bothering me too much.

I initially had a NavMesh baked along the road in my game.But later I had to change the layout of the roads, hence I had to rebake the Nav Mesh.

But now I'm having the problem where the agents are not moving along the Nav Mesh to Reach the destination, but instead they "try" to move along the shortest path.As a result of this all agents gather to a point rather than moving along the track.I've shown below a picture to better illustrate my problem.

Issue I have

They try to move along the red line instead of going along the road and then getting to the destination.

I'm pretty confident that nothing's wrong with my code because it worked earlier and works well in other scenes.Is there anything I missed when re-baking the Nav Mesh?Any help to get the agents to move along the road is much appreciated? Thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ Maybe you have simply now added your terrain as NavMesh object and before you only had roads set as Navigation Static? All objects marked with Navigation Static will be baked into NavMesh. \$\endgroup\$ – ezez Nov 21 at 19:19
  • \$\begingroup\$ Since you didn't mention this, I expect you didn't use this... but here it goes. There is also option to setup areas (Areas tab of Navigation setup) and area costs. You could keep both roads and terrain walkable, but make the terrain have higher cost. \$\endgroup\$ – ezez Nov 21 at 19:23
  • \$\begingroup\$ So... let me know if my answer / comments helped you or not. \$\endgroup\$ – ezez Nov 22 at 14:43
  • 1
    \$\begingroup\$ Thanks a lot for your input....however I couldn't solve my issue even after checking all your suggesting....but it was a silly mistake from my side....since I used modular tracks to build my road there way a small separation in one of the junctions which made the navmesh incomplete and hence the abnormal behaviour ....thanks again \$\endgroup\$ – AfiJaabb Nov 23 at 10:31
1
\$\begingroup\$

To make your NavMesh agents walk only on roads you have a few choices:

  1. Only make roads walkable. This can be done by setting only road GameObjects to use Navigation Area "Walkable" and make terrain use "Not Walkable" area. Check "Navigation Static" for both to include both in NavMesh generation. These settings can be found in Navigation panel's "Object" tab.

  2. If you have separate road mesh and it isn't intersected by any other object (which you would consider to be an obstacle for an agent), I think you could possibly only set road GameObject as Navigation Static and set its Navigation Area as "Walkable" and ignore the terrain GameObject completely.

  3. Make both roads and terrain use Navigation Area "Walkable" but set the terrain to have higher cost. You can define areas and set their costs from Navigation panel's "Areas" tab. Define a new area for terrain and make it have higher cost. Also set your terrain meshes to use this area from Navigation panel's "Object" tab (Navigation Area dropdown). Note that if your path along road is very long and target is near agent, agent might take the short path across terrain mesh.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.