Pathfinding completely shuts down and I can't figure out why

I’m having a very peculiar issue. I recently updated to Unity 2022.3.25f1 and A* Pathfinding Project Pro 5.0.9. I also updated my movement interaction for enemy agents to only call the aiPath.destination once per move request (previously was calling it in Update) or use AI Destination setter when gameObject/transform needs to be continuously tracked (ie. when the destination is not Vector2/3).

When used on their own, my agent pathfinding works well, regardless of the number of agents I throw in the scene, they keep following the player when need to, go to other coordinates etc. However when they’re members of my squad object (which sometimes changes parameters on the agents or directly asks them to move them to different locations) my whole pathfdinging suddenly get shut down shortly after initiating play mode. This means the paths dont’ seem to get updated (those visual debugging lines don’t have the blue circles in them for instance or they’re elsewhere than at the end of path). Weirdly enough, even if I drop completely fresh unrelated agent to the scene, they can’t move either. This is the same regardless of whether they use the AI Destination setter either, so even the agents that have transform properly set in their AI Destination Setter (which should be updating every frame) are not able to move. Sometimes the agents that would like to move through a path just circle in a small less than 1 unit circle while not being able to move further.

My project is a 2D one using Grid graph. I’m using aiPath scripts on them with Seekers (obviously), AI Destionation Setters, Simple Smooth Modifiers and RVO Controllers - disabling the latter two seemingly had no effect to the issue. I’ve unticked the “Stop when destination is crowded” option. From the debug options it seems that the agents are stuck in various different steps - some have reached an end of path but not the destionation, some have path pending and don’t even start to calculate it (especially if I insert completely fresh unit).

Obviously I’ve gone through my squad script but it seems to not be doing anything that would explain this, in fact it’s not doing anything at all at this point and the agents are completely trying to move according to their own AI but no avail. Weirdly enough I also discovered if I hit the scan button on the A* gameObject with the gridgraph, that causes every agent to move one like one segment of the path, then stop again.

I’ve been stuck on this for a week now and would really appreciate anything I could do to start figuring out what’s causing this, thanks for any help!

What I’d recommend is copious debugging. Your use case sounds very similar to mine, GridGraphs, bespoke pathfinding agents, AIPath and Seeker. My agents have very dynamic pathfinding, lots of changing destinations all based on different runtime dynamics. Implementing code changes is invariably going to lead to breakages, and adding a robust layer of debugging will help you track down where the problem is. Unfortunately, the odds that the problem is on your end, as opposed to a random bug in AIPath for exmaple, are high (which is to say the bug is probably on your end). Even if it’s not, the debugging layer will help you identify the location of the problem. From your description of the problem, it doesn’t sound like you’ve narrowed anything down enough (but that’s ok, adding more debugging will pay dividends down the road).

Having said all that, I’d recommend adding some debugging around setting destinations if you haven’t already. I set AIPath.destination directly, so I have a fair amount of debugging around what is being set, previous value(s), and why. You’re also in a good place to take advantage of OnPathComplete. Even if you don’t need this explicitly, you can extend it easily to debug what is occurring when your agents get to the end of their paths. Having this in place will give you some good signal as to whether your agents have arrived at their desired destination, and what they are going to do next.

You say that your agents have stopped, there’s quite a few values in AIPath that you can introspect to get a much richer idea of what the current state of your pathfinding agents are. For example, what are the values of AIPath.canMove, AIPath.isStopped, and AIPath.updatePosition? You can also write some functions to test against whether your agents are choosing GraphNodes on your GridGraphs that are walkable.

You’ve probably done a good chunk of this already, but if not then I’d encourage you to take the opportunity to add whatever you feel you need (the more you’re customizing the pathfinding of your agents, the more debugging will most likely help you).

Don’t forget you can also turn gizmos on to visualize both your GridGraph(s), and the paths your Seekers are taking (for example, is your GridGraph built out the way you assume it is?). This is invaluable in tracking down bugs. And if that’s not doing enough to help you, then you might also want to check out drawing your own debugging Gizmos. I actually use @aron_granberg’s ALine to visualize different aspects and states of my pathfinding agents when needed, but there are other drawing tools out there such as Shapes. You can also just drop sprite GameObjects into the scene with different colors and shapes to help visual state. I’ve tried all three approaches, starting with dropping GameObjects into the scene, and have found ALine to be the best option for me (easiest, and most straightforward to use), and Shapes to be unnecessarily complex and cumbersome for my use case.

Thanks so much for suggestions. Clearly it’s something I’m inadvertedly doing, debugging has been on my mind too and I’ve tried to build the new movement with debugging layer. So far I don’t see anything there that would point out to anything but I’ll keep digging. Thanks for giving me a lot of new ideas of what to look at.

In the meantime, what maybe puzzles me the most is how the entire pathfinding seems to be shut down, even to the point where new unrelated agents can’t use it, and further, why scanning the graphs seems to alliviate the issue for a second - would @krice you or @aron_granberg have any tips how to debug or see what’s going on with the entire pathfinding, not just individual agents? Any pointers again greatly appreciated, thanks.

Can you describe in further detail what it is you’re doing with your agents? You say they work, then they don’t, then you rescan your graphs and things work for a short period of time, then stop. I’m assuming by “rescan” you’re referring to AstarPath.Scan, but maybe you can verify that is what you’re calling since there are other ways to update graphs.

I would actually suggest not rescanning all your graphs while debugging (again, I’m assuming you’re making a call to, and instead focus on why your agents aren’t moving. Have you turned gizmos on and verified the paths your agents are trying to take? Have you verified that your GridGraph has been built out correctly by visually inspecting it? It’s possible that for some reason your agent’s destination lays outside of your GridGraph, in which case they may be refusing to move. I test for this fairly aggressively to ensure that the destination I’m providing to my agents is on a walkable node. Another question to ask would be, “do my agents think they’ve reached their destination or reached the end of their path”? Maybe you can ask them if they even have a path. Again, I suggest looking through AIPath and picking out some methods to call that can give you a better understanding of your agent’s current state. Have you tried overriding OnPathComplete yet?

At this point it sounds like you have some assumptions about the current state of your agents; what I recommend is testing those assumptions by calling in to AIPath and reviewing your agents current state.

If you haven’t already, you can also toggle debug mode in the Inspector. This will allow you to see the values of private/protected fields of AIPath, and the Seeker.

It’s a bit complex to describe. There’s a behavior tree AI component driving each enemy unit. When they are 100% controlled by their individual AIs, everything seems to work and everything run indefinitely as they should - the agents can change to various actions that call the pathfinding to move somewhere through my movement script, which will utilize the aiPath.destination in case of Vector value, or set the transform in if it’s transform/gameObject they need to navigate to.

I have a squad object that the units sometimes belong to. The squad might take over the unit and command them directly using the same movement script and same logic - during these calls the individual AI is not doing anything.

I realize there’s gotta be some conflict between these two that I’ve missed or left over, but so far on the surface everythign should be working as intended so again it comes down to trying to pinpoint the issue. Thanks again for the tips, you’ve given me a lot of things I’m working through to test and see if I could figure out if something gets called somewhere that shouldn’t.

So far using the various debugging methods I’ve noticed

  • when the agents stop, their “wait for path calculation” remains ticked, visually showing the path calculated but the agent doesn’t take actions to move
  • I have some dynamic grid obstacles on the scene as well which seem to also stop updating the grid once the enemy units stop again hinting this issue is plaguing the whole pathfinding system. Still having hard time understanding how potentially conflicting instructions to single agents would stop the whole system from updating

Not much else has really yielded any results yet, I gotta dig in deeper into what an individual agent is trying to do at each moment. I do see a lot of cases though where they have a path visually displayed but they just don’t move along it or hover in small circle.

By the way, how do you handle your agents stopping when they reach destination and have nothing to do? I recently added function to turn the aiPath.canMove off when reaching destination so they wouldn’t have anything pending but I’m now wondering if that would be somehow interfiering with the next move request. I’m naturally turning this value back to true before taking in move request but I guess there might be something else turning it off… though, again this is single agent behavior and I can’t understand how that would affect the grid level/whole pathfinding system level behavior I’m witnessing…

I can also confirm once the system gets stuck, new path requests for agents don’t get processed anymore, ie. even though an agent was set a new destination (0,0, covered by the gridgraph and accessible) via aiPath.destination, the pathfinding doesn’t calculate the path for the agent (the old remaining path remains visually on the scene, agent doesn’t move.

Again getting the impression whole pathfinding system is disabled and I don’t understand what could cause it, when I’m only calling individual agents…

So I make use of OnPathComplete to govern what my agents do when they have completed a path. For example, was there an error with the path(?), and if so process the error:

if (p != null && p.error)
        this.ProcessPathError(path: p);

I also test for whether the pathfinding agent is active and enabled, whether the agent has reached its destination (taking action if it has), and if the agent needs to request a new destination, if it does then I’ll calculate and set it. Worth mentioning that I doubt your paths have errors, so I wouldn’t zero in specifically on that; be broad in your approach to identifying the state of your agents when they’ve completed their paths.

Have you wired up OnPathComplete yet? I think you’ll find it provides functionality that it sounds like you need from the description of your set up.

There’s also plenty of functionality available to you from your agent’s Seeker. You might also want to read the Pathfinding docs, specficially the section on calling the Seeker.

Oh, and one little trick you can do to test your hypothesis of whether the pathfinding system of an agent has broken (because you observe that it isn’t moving), is to go into the scene view in the editor, and manually move the Transform of the agent (just drag the Transform handles a little to the left/right, or up/down, and then release). My guess is that if you do this you’ll see the agent move back to the position it had previously stopped at. This can indicate that you need to focus your attention on what the agent is doing when its path is complete.