First off, I absolutely love A* - it’s awesome and I’ve been using it for many years. I’m working on a 2D game and running into an issue that all of my forum searches and ridiculous attempts at trying everything haven’t been able to solve.
NPCs move around fine, follow paths based on a scheduling system I implemented, but for whatever reason this one NPC Hans just won’t have any of it. I’m fairly certain this is a global issue - I’m just mad at Hans.
There’s obviously a ton of code behind all this, so I’ll do my best to break down the issue:
- Hans is happily walking along a beautiful A* path within my 2D world. When I quit the game, I save his current position, starting position, and target position (all within the node grid).
- When I reload the game, I set his AStarAI destination (ai.destination) to the saved target position and he just does the moonwalk in place.
- I’m getting following error: Path Failed : Computation Time 0.00 ms Searched Nodes 0
Error: Couldn’t find a node close to the end point. I looked at your docs on this, and I made sure that the starting and end position are within the node graph (Debug.Logging to make sure these numbers are correct).
I’m happy to provide some code if that’s helpful, but I know that’s probably outside of the scope of your support. Here is my grid setup:
This image is of Hans nicely following a path. I included this so you could see the graph, but also for the console log, as I thought it was a bit weird he was repathing so frequently and sometimes had failtures. He gets to his destination and all is fine, but when I reload the game and start him on his path again, he just walks in place and I get the error above.
This one is of my pathfinding setup.
By no means am I looking for you to solve my issue, but perhaps a little nudge in the right direction with something I could be missing.
(Note: I do have my grid set up for 3D even though it’s 2D… I just rotate the character gameobjects within a parent that has Seeker and AIPath components on it)
Happy to provide any additional information that you could use to help me solve my problem.
Thanks again for a great product - would absolutely recommend it to anyone.
I cannot see anything obviously wrong from your screenshots. Do you think you could try:
var startNode = AstarPath.active.GetNearest(ai.position, NNConstraint.Default).node;
var endNode = AstarPath.active.GetNearest(ai.destination, NNConstraint.Default).node;
Debug.DrawRay((Vector3)startNode.position, Vector3.forward, Color.red, 1f);
Debug.DrawRay((Vector3)endNode.position, Vector3.forward, Color.blue, 1f);
Debug.Log(startNode.Area + " " + endNode.Area);
and see what you get?
Thanks for getting back to me. The debug doesn’t appear to be drawing any lines, but the result of the last debug is startNode.Area = 2 ; endNode.Area = 1. That give us anything to work with?
That does indicate that they are not reachable from each other.
Are you sure the debug lines are not visible? Maybe try using
Vector3.forward * 100?
You can use
A* Inspector -> Settings -> Graph Coloring = Areas to show the different connected components of the graph.
Thanks for helping out with this… the graph coloring isn’t really showing me much - except that the start and end points are connected
I’m pretty sure the issue is coming from trying to restart the path once the scene has changed. So for example, if I leave the scene before Hans, he walks out the door and continues his path just fine. But if he leaves first then I go outside he’s just walking in place. I’m wagering something is wrong on my end with the code, but it works in other instances, which is frustrating. When the scene loads I am doing the following:
- Check if the ai has reached its destination
- If not, setting the ai.destination to the target destination
That should work, right?
I’ve also validated Hans’s gameobject and the target positiona are valid areas on the grid…
Thanks again for troubleshooting with me. Let me know what other information I could provide.
Why not always set the destination? It cannot know if it has reached its destination unless you have a destination set.
I do always set the destination. Is there anything I need to do regarding searching paths or anything, or is just setting ai.destination to a Vector3 good enough?
If that is the case, then the start node and end node should be drawn with different colors in the scene view (when you have Graph Coloring = Areas). So I have a feeling your destination or agent position is not where you think it is.
Setting ai.destination should be good enough.