This example shows how to use the Pathfinding::XPath's EndingCondition variable.In this example a custom ending condition class is created which tells the path to stop when a node is close enough to the target point. This script should be attached to a GameObject with a Seeker component.
- Version
- Tested in the A* Pathfinding Project 3.0.7
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
public class EndingCondition : MonoBehaviour {
#if FALSE
public int hLimit = 100;
public Transform targetPoint;
void Start () {
Seeker seeker = GetComponent<Seeker>();
XPath p = new XPath (transform.position,targetPoint.position,null);
p.endingCondition = new CEC (p, hLimit);
Debug.DrawLine (transform.position,targetPoint.position,Color.black);
}
public class CEC : ABPathEndingCondition {
public int hLimit = 80;
public CEC (ABPath p, int lim) : base (p) {
hLimit = lim;
}
public override bool TargetFound (PathNode node)
{
return node.H < hLimit || node.node == abPath.endNode;
}
}
public void OnPathComplete (Path p) {
Debug.Log ("Got Callback");
if (p.error) {
Debug.Log ("Ouch, the path returned an error");
return;
}
List<Vector3> path = p.vectorPath;
for (int j=0;j<path.Count-1;j++) {
Debug.DrawLine (path[j],path[j+1],Mathfx.IntToColor (1,0.5F));
}
}
#endif
}