Example EndingCondition.cs

This example shows how to use the Pathfinding::XPath's EndingCondition variable.

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.


Tested in the A* Pathfinding Project 3.0.7

using UnityEngine;
using System.Collections.Generic;

namespace Pathfinding.Examples {
public class EndingCondition : MonoBehaviour {
public int hLimit = 1000;

public Transform targetPoint;

public void Start () {
Seeker seeker = GetComponent<Seeker>();

seeker.pathCallback = OnPathComplete;

// Create a new ABPath with a custom ending condition
var p = ABPath.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);


public class CustomEndingCondition : ABPathEndingCondition {
public int hLimit = 10000;

public CustomEndingCondition (ABPath path, int lim) : base(path) {
hLimit = lim;

public override bool TargetFound (GraphNode node, uint H, uint G) {
return H < hLimit || node == abPath.endNode;

public void OnPathComplete (Path p) {
Debug.Log("Got Callback");

if (p.error) {
Debug.Log("Ouch, the path returned an error");

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);