A* Pathfinding Project
4.0.8
The A* Pathfinding Project for Unity 3D
|
Moves a grid graph to follow a target. More...
Moves a grid graph to follow a target.
Attach this to some object in the scene and assign the target to e.g the player. Then the graph will follow that object around as it moves.
This is useful if pathfinding is only necessary in a small region around an object (for example the player). It makes it possible to have vast open worlds (maybe procedurally generated) and still be able to use pathfinding on them.
When the graph is moved you may notice an fps drop. If this grows too large you can try a few things:
Make sure you have 'Show Graphs' disabled in the A* inspector since gizmos in the scene view can take some time to update when the graph moves and thus make it seem like this script is slower than it actually is.
Public Member Functions | |
void | UpdateGraph () |
Updates the graph asynchronously. | |
Public Attributes | |
bool | floodFill = true |
Flood fill the graph after updating. | |
Transform | target |
Graph will be moved to follow this target. | |
float | updateDistance = 10 |
Graph will be updated if the target is more than this number of nodes from the graph center. | |
Properties | |
bool | updatingGraph [get, set] |
True while the graph is being updated by this script. | |
Private Member Functions | |
Vector3 | PointToGraphSpace (Vector3 p) |
Transforms a point from world space to graph space. | |
void | Start () |
void | Update () |
Update is called once per frame. | |
IEnumerator | UpdateGraphCoroutine () |
Async method for moving the graph. | |
Private Attributes | |
GridNodeBase[] | buffer |
Temporary buffer. | |
GridGraph | graph |
Grid graph to update. | |
Additional Inherited Members | |
Protected Member Functions inherited from VersionedMonoBehaviour | |
virtual void | Awake () |
virtual int | OnUpgradeSerializedData (int version) |
Handle serialization backwards compatibility. | |
|
private |
Transforms a point from world space to graph space.
In graph space, (0,0,0) is bottom left corner of the graph and one unit along the X and Z axes equals distance between two nodes the Y axis still uses world units
|
private |
|
private |
Update is called once per frame.
void UpdateGraph | ( | ) |
Updates the graph asynchronously.
This will move the graph so that the target's position is the center of the graph. If the graph is already being updated, the call will be ignored.
The image below shows which nodes will be updated when the graph moves. The whole graph is not recalculated each time it is moved, but only those nodes that have to be updated, the rest will keep their old values. The image is a bit simplified but it shows the main idea.
If you want to move the graph synchronously then call
Immediately after you have called this method.
|
private |
Async method for moving the graph.
|
private |
Temporary buffer.
bool floodFill = true |
Flood fill the graph after updating.
If this is set to false, areas of the graph will not be recalculated. Disable this only if the graph will only have a single area (i.e from all walkable nodes there is a valid path to every other walkable node). One case where this might be appropriate is a large outdoor area such as a forrest. If there are multiple areas in the graph and this is not enabled, pathfinding could fail later on.
Disabling flood fills will make the graph updates faster.
|
private |
Grid graph to update.
Transform target |
Graph will be moved to follow this target.
float updateDistance = 10 |
Graph will be updated if the target is more than this number of nodes from the graph center.
Note that this is in nodes, not world units.
|
getset |
True while the graph is being updated by this script.