A* Pathfinding Project
4.1.20
The A* Pathfinding Project for Unity 3D
|
Handles path calls for a single unit. More...
Handles path calls for a single unit.
This is a component which is meant to be attached to a single unit (AI, Robot, Player, whatever) to handle its pathfinding calls. It also handles post-processing of paths using modifiers.
Public Types | |
enum | ModifierPass { PreProcess, PostProcess = 2 } |
Public Member Functions | |
Seeker () | |
void | CancelCurrentPathRequest (bool pool=true) |
Stop calculating the current path request. More... | |
void | DeregisterModifier (IPathModifier modifier) |
Called by modifiers when they are disabled or destroyed. More... | |
Path | GetCurrentPath () |
Path that is currently being calculated or was last calculated. More... | |
ABPath | GetNewPath (Vector3 start, Vector3 end) |
Returns a new path instance. More... | |
bool | IsDone () |
Is the current path done calculating. More... | |
void | OnDestroy () |
Cleans up some variables. More... | |
void | OnDrawGizmos () |
Draws gizmos for the Seeker. More... | |
PathResult | PostProcess (Path path) |
Post Processes the path. More... | |
void | RegisterModifier (IPathModifier modifier) |
Called by modifiers to register themselves. More... | |
void | RunModifiers (ModifierPass pass, Path path) |
Runs modifiers on a path. More... | |
void | SetModifiers (Path path) |
Makes the path use the modifiers associated with this Seeker. More... | |
MultiTargetPath | StartMultiTargetPath (Vector3 start, Vector3[] endPoints, bool pathsForAll, OnPathDelegate callback=null, int graphMask=-1) |
Starts a Multi Target Path from one start point to multiple end points. More... | |
MultiTargetPath | StartMultiTargetPath (Vector3[] startPoints, Vector3 end, bool pathsForAll, OnPathDelegate callback=null, int graphMask=-1) |
Starts a Multi Target Path from multiple start points to a single target point. More... | |
MultiTargetPath | StartMultiTargetPath (Vector3 start, Vector3[] endPoints, bool pathsForAll, Action< MultiTargetPathResult > callback) |
MultiTargetPath | StartMultiTargetPath (Vector3[] startPoints, Vector3 end, bool pathsForAll, Action< MultiTargetPathResult > callback) |
MultiTargetPath | StartMultiTargetPath (MultiTargetPath p, OnPathDelegate callback=null, int graphMask=-1) |
Starts a Multi Target Path. More... | |
Path | StartPath (Vector3 start, Vector3 end, OnPathDelegate callback=null) |
Call this function to start calculating a path. More... | |
Path | StartPath (Vector3 start, Vector3 end, OnPathDelegate callback, int graphMask) |
Call this function to start calculating a path. More... | |
Path | StartPath (Path p, OnPathDelegate callback=null) |
Call this function to start calculating a path. More... | |
Path | StartPath (Path path, OnPathDelegate callback, int graphMask) |
Call this function to start calculating a path. More... | |
void | StartPath (Vector3 start, Vector3 end, Action< ABPathResult > callback) |
void | StartPath (ABPath path, Action< ABPathResult > callback) |
Public Attributes | |
bool | detailedGizmos |
Enables drawing of the non-postprocessed path using Gizmos. More... | |
bool | drawGizmos = true |
Enables drawing of the last calculated path using Gizmos. More... | |
int | graphMask = -1 |
Graphs that this Seeker can use. More... | |
OnPathDelegate | pathCallback |
Callback for when a path is completed. More... | |
OnPathDelegate | postProcessPath |
Called after a path has been calculated, right before modifiers are executed. More... | |
OnPathDelegate | preProcessPath |
Called before pathfinding is started. More... | |
StartEndModifier | startEndModifier = new StartEndModifier() |
Path modifier which tweaks the start and end points of a path. More... | |
int [] | tagPenalties = new int[32] |
Penalties for each tag. More... | |
int | traversableTags = -1 |
The tags which the Seeker can traverse. More... | |
Protected Member Functions | |
override void | Awake () |
Initializes a few variables. More... | |
Protected Member Functions inherited from VersionedMonoBehaviour | |
virtual int | OnUpgradeSerializedData (int version, bool unityThread) |
Handle serialization backwards compatibility. More... | |
Protected Attributes | |
uint | lastPathID |
The path ID of the last path queried. More... | |
Path | path |
The current path. More... | |
Private Member Functions | |
void | OnPathComplete (Path p) |
Called when a path has completed. More... | |
void | SetDefaultGraphMask (Path path) |
Set the graph mask only if the user has not changed it from the default value. More... | |
void | StartPathInternal (Path p, OnPathDelegate callback, Action< PathResult > callback2=null, Action< ABPathResult > callback3=null, Action< MultiTargetPathResult > callback4=null) |
Internal method to start a path and mark it as the currently active path. More... | |
Static Private Member Functions | |
static void | TryCallAction< T > (Action< T > action, T value) |
Private Attributes | |
readonly List< IPathModifier > | modifiers = new List<IPathModifier>() |
Internal list of all modifiers. More... | |
readonly OnPathDelegate | onPartialPathDelegate |
Cached delegate to avoid allocating one every time a path is started. More... | |
readonly OnPathDelegate | onPathDelegate |
Cached delegate to avoid allocating one every time a path is started. More... | |
OnPathDelegate | tmpPathCallback |
Temporary callback only called for the current path. More... | |
Action< PathResult > | tmpPathCallback2 |
Temporary callback only called for the current path. More... | |
Action< ABPathResult > | tmpPathCallback3 |
Temporary callback only called for the current path. More... | |
Action< MultiTargetPathResult > | tmpPathCallback4 |
Temporary callback only called for the current path. More... | |
|
strong |
Seeker | ( | ) |
|
protectedvirtual |
Initializes a few variables.
Reimplemented from VersionedMonoBehaviour.
void CancelCurrentPathRequest | ( | bool | pool = true | ) |
Stop calculating the current path request.
If this Seeker is currently calculating a path it will be canceled. The callback (usually to a method named OnPathComplete) will soon be called with a path that has the 'error' field set to true.
This does not stop the character from moving, it just aborts the path calculation.
pool | If true then the path will be pooled when the pathfinding system is done with it. |
void DeregisterModifier | ( | IPathModifier | modifier | ) |
Called by modifiers when they are disabled or destroyed.
Path GetCurrentPath | ( | ) |
Path that is currently being calculated or was last calculated.
You should rarely have to use this. Instead get the path when the path callback is called.
ABPath GetNewPath | ( | Vector3 | start, |
Vector3 | end | ||
) |
Returns a new path instance.
The path will be taken from the path pool if path recycling is turned on.
This path can be sent to StartPath(Path,OnPathDelegate,int) with no change, but if no change is required StartPath(Vector3,Vector3,OnPathDelegate) does just that.
bool IsDone | ( | ) |
Is the current path done calculating.
Returns true if the current path has been returned or if the path is null.
void OnDestroy | ( | ) |
Cleans up some variables.
Releases any eventually claimed paths. Calls OnDestroy on the startEndModifier.
void OnDrawGizmos | ( | ) |
Draws gizmos for the Seeker.
|
private |
Called when a path has completed.
Will post process it and return it by calling tmpPathCallback and pathCallback
PathResult PostProcess | ( | Path | path | ) |
Post Processes the path.
This will run any modifiers attached to this GameObject on the path. This is identical to calling RunModifiers(ModifierPass.PostProcess, path)
void RegisterModifier | ( | IPathModifier | modifier | ) |
Called by modifiers to register themselves.
void RunModifiers | ( | ModifierPass | pass, |
Path | path | ||
) |
Runs modifiers on a path.
|
private |
Set the graph mask only if the user has not changed it from the default value.
void SetModifiers | ( | Path | path | ) |
Makes the path use the modifiers associated with this Seeker.
Normally you do not need to do this, however if you are calculating paths without using the Seeker, but nonetheless want to use modifiers to post process the path then you can use this method to make the path use the same modifiers as the Seeker.
MultiTargetPath StartMultiTargetPath | ( | Vector3 | start, |
Vector3 [] | endPoints, | ||
bool | pathsForAll, | ||
OnPathDelegate | callback = null , |
||
int | graphMask = -1 |
||
) |
Starts a Multi Target Path from one start point to multiple end points.
A Multi Target Path will search for all the end points in one search and will return all paths if pathsForAll is true, or only the shortest one if pathsForAll is false.
start | The start point of the path |
endPoints | The end points of the path |
pathsForAll | Indicates whether or not a path to all end points should be searched for or only to the closest one |
callback | The function to call when the path has been calculated |
graphMask | Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask. |
callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
MultiTargetPath StartMultiTargetPath | ( | Vector3 [] | startPoints, |
Vector3 | end, | ||
bool | pathsForAll, | ||
OnPathDelegate | callback = null , |
||
int | graphMask = -1 |
||
) |
Starts a Multi Target Path from multiple start points to a single target point.
A Multi Target Path will search from all start points to the target point in one search and will return all paths if pathsForAll is true, or only the shortest one if pathsForAll is false.
startPoints | The start points of the path |
end | The end point of the path |
pathsForAll | Indicates whether or not a path from all start points should be searched for or only to the closest one |
callback | The function to call when the path has been calculated |
graphMask | Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask. |
callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
MultiTargetPath StartMultiTargetPath | ( | Vector3 | start, |
Vector3 [] | endPoints, | ||
bool | pathsForAll, | ||
Action< MultiTargetPathResult > | callback | ||
) |
MultiTargetPath StartMultiTargetPath | ( | Vector3 [] | startPoints, |
Vector3 | end, | ||
bool | pathsForAll, | ||
Action< MultiTargetPathResult > | callback | ||
) |
MultiTargetPath StartMultiTargetPath | ( | MultiTargetPath | p, |
OnPathDelegate | callback = null , |
||
int | graphMask = -1 |
||
) |
Starts a Multi Target Path.
Takes a MultiTargetPath and wires everything up for it to send callbacks to the seeker for post-processing.
p | The path to start calculating |
callback | The function to call when the path has been calculated |
graphMask | Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask. |
callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
Path StartPath | ( | Vector3 | start, |
Vector3 | end, | ||
OnPathDelegate | callback = null |
||
) |
Call this function to start calculating a path.
start | The start point of the path |
end | The end point of the path |
callback | The function to call when the path has been calculated |
callback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
Path StartPath | ( | Vector3 | start, |
Vector3 | end, | ||
OnPathDelegate | callback, | ||
int | graphMask | ||
) |
Call this function to start calculating a path.
start | The start point of the path |
end | The end point of the path |
callback | The function to call when the path has been calculated |
graphMask | Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask. This will override graphMask for this path request. |
callback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
Path StartPath | ( | Path | p, |
OnPathDelegate | callback = null |
||
) |
Call this function to start calculating a path.
p | The path to start calculating |
callback | The function to call when the path has been calculated |
The callback will be called when the path has been calculated (which may be several frames into the future). The callback will not be called if a new path request is started before this path request has been calculated.
Path StartPath | ( | Path | path, |
OnPathDelegate | callback, | ||
int | graphMask | ||
) |
Call this function to start calculating a path.
p | The path to start calculating |
callback | The function to call when the path has been calculated |
graphMask | Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask. This will override graphMask for this path request. |
The callback will be called when the path has been calculated (which may be several frames into the future). The callback will not be called if a new path request is started before this path request has been calculated.
void StartPath | ( | Vector3 | start, |
Vector3 | end, | ||
Action< ABPathResult > | callback | ||
) |
void StartPath | ( | ABPath | path, |
Action< ABPathResult > | callback | ||
) |
|
private |
Internal method to start a path and mark it as the currently active path.
|
staticprivate |
bool detailedGizmos |
Enables drawing of the non-postprocessed path using Gizmos.
The path will show up in orange.
Requires that drawGizmos is true.
This will show the path before any post processing such as smoothing is applied.
bool drawGizmos = true |
Enables drawing of the last calculated path using Gizmos.
The path will show up in green.
int graphMask = -1 |
Graphs that this Seeker can use.
This field determines which graphs will be considered when searching for the start and end nodes of a path. It is useful in numerous situations, for example if you want to make one graph for small units and one graph for large units.
This is a bitmask so if you for example want to make the agent only use graph index 3 then you can set this to:
Note that this field only stores which graph indices that are allowed. This means that if the graphs change their ordering then this mask may no longer be correct.
If you know the name of the graph you can set the mask like this:
Some overloads of the StartPath methods take a graphMask parameter. If those overloads are used then they will override the graph mask for that path request.
|
protected |
The path ID of the last path queried.
|
private |
Internal list of all modifiers.
|
private |
Cached delegate to avoid allocating one every time a path is started.
|
private |
Cached delegate to avoid allocating one every time a path is started.
|
protected |
The current path.
OnPathDelegate pathCallback |
Callback for when a path is completed.
Movement scripts should register to this delegate.
A temporary callback can also be set when calling StartPath, but that delegate will only be called for that path
OnPathDelegate postProcessPath |
Called after a path has been calculated, right before modifiers are executed.
OnPathDelegate preProcessPath |
Called before pathfinding is started.
StartEndModifier startEndModifier = new StartEndModifier() |
Path modifier which tweaks the start and end points of a path.
int [] tagPenalties = new int[32] |
Penalties for each tag.
Tag 0 which is the default tag, will have added a penalty of tagPenalties[0]. These should only be positive values since the A* algorithm cannot handle negative penalties.
|
private |
Temporary callback only called for the current path.
This value is set by the StartPath functions
|
private |
Temporary callback only called for the current path.
This value is set by the StartPath functions
|
private |
Temporary callback only called for the current path.
This value is set by the StartPath functions
|
private |
Temporary callback only called for the current path.
This value is set by the StartPath functions
int traversableTags = -1 |