A* Pathfinding Project
3.7.3
The A* Pathfinding Project for Unity 3D
|
The pathfinding can be called in a number of ways. The by far easiest method is to have Seeker component attached to the GameObject you want to call pathfinding from and then call Seeker::StartPath.
Note a Seeker will only take one pathfinding call at a time, if you send one before the previous one has been completed, it the previous one will be canceled
The Seeker will also automatically handle modifiers.
A common mistake is to assume that the path is already calculated right after the StartPath call. This is however incorrect. The StartPath call will only put the path in a queue. This is done because when many units are calculating their paths at the same time, we want to spread out the path calculations over several frames to avoid FPS drops. We can also calculate the paths in other threads if multithreading has been enabled. There are of course cases where you need to calculate the path immediately. Then you can use the AstarPath.WaitForPath method. It will block until that path has been calculated. You can also wait for the path to be calculated in a coroutine using Pathfinding.Path.WaitForPath.
You can also create your own path objects instead of using the Seeker's methods.
This will enable you to change settings on the path object before calculating it.
It might seem tedious to specify the callback function every time, well, you don't have to.
There is a field on the Seeker which can be set to a function which will be called every time a path is returned. For those of you interested in performance, this is also a bit more memory efficient since it does not allocate a new delegate on the heap every time.
A nice thing is that the callback is not limited to one function, you can have several scripts which all get a callback, or just callbacks to more than one function in the script (if you for some reason need that).
That's why I have been using + and - when registering and un-registering the callback.
There are other types of paths than the standard one, for example the MultiTargetPath (Pro feature).
These can be started easily as well, especially the MultiTargetPath since the Seeker has a special function for it
The path will be returned as a Path instance, but it can be casted to for example MultiTargetPath to get all the data. A complete example of MultiTargetPath usage can be found here: Multi Target Paths
The generic way to start a type of path is simply
This is instead of a real consructor. It is used so that path pooling can be done more easily.
There are of course cases where you want even more control of each path. Then you can call AstarPath directly. The main function you then use is AstarPath.StartPath. This can be done if you want to calculate a lot of paths at the same time. The Seeker is for agents that only have one active path at a time and if you try to request multiple paths at the same time it will only calculate the last one and cancel the rest.