Simple Bug Fix for A* Pathfinding Integration

zodd

New member
Hello!

In your Seek behavior (and possibly other behaviors) for this integration, you have the following code:

C#:
        protected override bool SetDestination(Vector3 target)
        {
            agent.canSearch = true;
            agent.canMove = true;
            agent.destination = target;
            agent.SearchPath();
            return true;
        }

This code works in many situations where the path is able to be calculated within a single frame. However, on a slower computer, or when working with +150 paths on even a very high end computer, paths start to require more than a single frame to calculate. When the Seek behavior loops and calls this function a second time before the first path has finished calculating, it cancels the original path before it begins. The result is paths always being cancelled before they can be created and the GameObject thus never moves.

I have implemented a very simple fix that I think you should implement in your code to resolve this for future clients to save them the headache.

C#:
        protected override bool SetDestination(Vector3 target)
        {
           if(!agent.pathPending)
            {
                agent.canSearch = true;
                agent.canMove = true;
                agent.destination = target;
                agent.SearchPath();     
            }
            return true;
        }

This solved the issue for me and now I am able to generate infinite paths until my computer crashes :D
 
Thanks! I wonder though in this case the destination may not be updated if the path is searching so could you do something like:

Code:
           agent.canSearch = true;
           agent.canMove = true;
           agent.destination = target;
           if(!agent.pathPending)
            {
                agent.SearchPath();     
            }
            return true;
 
Top