A* Pathfinding Project

Prevent hovering above navmesh (but more efficient than GetNearest)


#1

Hey Aron,

is there an efficient way to push my agent towards the node underneath it?
My mobile game has elevations such as hills and stairs.
This code produces a lot of overhead (25% of the frame), because it’s done for each character, I have around 25 of them. They just need to stay on navmesh, without any expensive Physics Raycasting.

NNInfo info = AstarPath.active.GetNearest( wanted_nextAi_pos,  constraint,  _nearest_pathfindNodeHint );
_nearest_pathfindNodeHint =  info.node;//update "hint" for more performance during Clamp next time.
wanted_nextAI_pos =  info.position;

Looking one of such 25 “tails” in more detail, we can see that GetNearest() takes a lot of time, even with a hint:

Currently I am using a simple ABPath.cs, perhaps there is a modifier I could attach?
Or maybe I should inherit from ABPath and override MovementUpdateInternal(). But it looks hard to change, especially that it’s using Velocity2D. How should I adjust it correctly?

Thank you!


#2

Hi

You could possibly use unity’s new RaycastCommand to do the raycast more efficiently and multithreaded?

Grid graphs are also usually faster for finding the closest node. Though I realize it might be a bit annoying to switch to a different graph type.

You can also enable Recast Graph settings -> Nearest Node Queries In XZ Space. This is faster, but it may not work as well if you have multiple floors in your game (e.g. a multi-storey building).
See https://arongranberg.com/astar/docs/navmeshbase.html#nearestSearchOnlyXZ

One thing I did in a different game (nothing included in A* Pathfinding Project) was to use a raycast every 10 frames or something and approximate the ground using a plane. Then in the other 9 frames I would simulate a raycast against the ground plane to approximate where the ground would be.