A* Pathfinding Project  4.2.7
The A* Pathfinding Project for Unity 3D
PathEndingCondition Class Referenceabstract

Customized ending condition for a path. More...

Detailed Description

Customized ending condition for a path.

This class can be used to implement a custom ending condition for e.g an Pathfinding.XPath. Inherit from this class and override the TargetFound function to implement you own ending condition logic.

For example, you might want to create an Ending Condition which stops when a node is close enough to a given point. Then what you do is that you create your own class, let's call it MyEndingCondition and override the function TargetFound to specify our own logic. We want to inherit from ABPathEndingCondition because only ABPaths have end points defined.

public class MyEndingCondition : ABPathEndingCondition {
// Maximum world distance to the target node before terminating the path
public float maxDistance = 10;
// Reuse the constructor in the superclass
public MyEndingCondition (ABPath p) : base (p) {}
public override bool TargetFound (PathNode node) {
return ((Vector3)node.node.position - abPath.originalEndPoint).sqrMagnitude <= maxDistance*maxDistance;
}
}

One part at a time. We need to cast the node's position to a Vector3 since internally, it is stored as an integer coordinate (Int3). Then we subtract the Pathfinding.Path.originalEndPoint from it to get their difference. The original end point is always the exact point specified when calling the path. As a last step we check the squared magnitude (squared distance, it is much faster than the non-squared distance) and check if it is lower or equal to our maxDistance squared.
There you have it, it is as simple as that. Then you simply assign it to the endingCondition variable on, for example an XPath which uses the EndingCondition.

XPath myXPath = XPath.Construct(startPoint, endPoint);
MyEndingCondition ec = new MyEndingCondition();
ec.maxDistance = 100; // Or some other value
myXPath.endingCondition = ec;
// Calculate the path!
seeker.StartPath (ec);

Where seeker is a #Seeker component, and myXPath is an Pathfinding.XPath.

Note
The above was written without testing. I hope I haven't made any mistakes, if you try it out, and it doesn't seem to work. Please post a comment in the forums.
Version
Method structure changed in 3.2
Updated in version 3.6.8
See also
Pathfinding.XPath
Pathfinding.ConstantPath

Public Member Functions

 PathEndingCondition (Path p)
 
abstract bool TargetFound (PathNode node)
 Has the ending condition been fulfilled. More...
 

Protected Member Functions

 PathEndingCondition ()
 

Protected Attributes

Path path
 Path which this ending condition is used on. More...
 

Constructor & Destructor Documentation

◆ PathEndingCondition() [1/2]

PathEndingCondition ( )
protected

◆ PathEndingCondition() [2/2]

Member Function Documentation

◆ TargetFound()

abstract bool TargetFound ( PathNode  node)
pure virtual

Has the ending condition been fulfilled.

Parameters
nodeThe current node.

Implemented in EndingConditionProximity, EndingConditionDistance, and ABPathEndingCondition.

Member Data Documentation

◆ path

Path path
protected

Path which this ending condition is used on.


The documentation for this class was generated from the following file: