A* Pathfinding Project  4.0.4
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
EndingCondition.cs

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.Generic;
namespace Pathfinding.Examples {
[RequireComponent(typeof(Seeker))]
public class EndingCondition : MonoBehaviour {
public int hLimit = 1000;
public Transform targetPoint;
void Start () {
Seeker seeker = GetComponent<Seeker>();
seeker.pathCallback = OnPathComplete;
// Create a new XPath with a custom ending condition
XPath p = XPath.Construct(transform.position, targetPoint.position, null);
p.endingCondition = new CustomEndingCondition(p, hLimit);
// Draw a line in black from the start to the target point
Debug.DrawLine(transform.position, targetPoint.position, Color.black);
seeker.StartPath(p);
}
public class CustomEndingCondition : ABPathEndingCondition {
public int hLimit = 10000;
public CustomEndingCondition (ABPath path, int lim) : base(path) {
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++) {
// Plot segment j to j+1 with a nice color got from Pathfinding.AstarMath.IntToColor
Debug.DrawLine(path[j], path[j+1], AstarMath.IntToColor(1, 0.5F), 1);
}
}
}
}