Navigation costs - Get units to walk along roads like humans

Using Unity NavMesh an agent will always take the shortest path, so if I have a long road with several corners, they will look like robots, and not humans, when going to their destination.

Is there some way using gridgraph in this package to achieve a more human result, where they wont always cut corners and walk along edges? Instead focus more on the center or the road, or even more randomly like a human.

This shows exactly what I mean:

Hi

You can do something similar in this package.

Either you can use a texture like in that video, or you can use a more automatic approach:

  1. Set grid graph => erosion iterations to something like 5.
  2. Enable ‘erosion uses tags’ and set the start tag to Tag 1.

This will mark the nodes right next to the wall with Tag 1, the ones 1 unit away from the wall with Tag 2, the ones 2 units away from the wall with Tag 3, and so on (up to 5 units away).

Then you can modify your Seeker -> Tags settings to add a penalty for moving close to walls.

(you may have to use higher or lower penalty values depending on the scale of your scene. 1000 is roughly the cost of moving 1 world unit).

Without penalties

With penalties

Thanks a lot for ur answer! Thats cool but:

  1. It seems in ur example ur using them for obstacle avoidance, while I would like to use a similar approach to have them walk on roads, would I use the same logic but inverse? (Or am I misudenrstanding, are the dark blue cells roads?)

  2. It seems this is applied everywhere in you scene, could I limit it to be applied to just the roads?

After some experimenting it does not seems to really work for what I need, being roads.

I can easily replicate what you did, and thats neat! However I want a unit to walk along a road.

Here is an example:

As you can see the erosion works nicely, however the road (GraphUpdateScene) has the same tag value everywhere, which means the agents will not choose to walk more towards the center, instead they just walk along the edges of the road since they think that is the best path.

What I want is what is in the video above, where the road has higher weights (or tag values) the further out on the road you get.

Hi

For that you need to use a texture (or possibly a custom script). You can find a texture option in the grid graph settings near the bottom.

This would be done by applying a high penalty for not walking on roads. Negative penalties are impossible to use with the A* algorithm, but you can use a penalty everywhere else to emulate a negative penalty.

Roads are placed dynamically by the players. From what I gathered using a texture is for static maps.

Is there some sample code anywhere where I can look at applying a texture dynamically to surrounding nodes? i.e when I place a road use the texture to set weights on surrounding nodes.

Hi

There is no example like that I’m afraid. For this I think you will need a custom script to set the node penalties.
See https://arongranberg.com/astar/docs/graph-updates.php#scripting

Okay, Ill try some stuff out! Thanks :slight_smile: