Full source for the simple AI written in the Get Started Guide.It should be attached to a GameObject with a Seeker and a CharacterController also attached.
using UnityEngine;
using System.Collections;
using Pathfinding;
public class AstarAI : MonoBehaviour {
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
public Path path;
public float speed = 100;
public float nextWaypointDistance = 3;
private int currentWaypoint = 0;
public float repathRate = 0.5f;
private float lastRepath = -9999;
public void Start () {
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
}
public void OnPathComplete (Path p) {
p.Claim (this);
if (!p.error) {
if (path != null) path.Release (this);
path = p;
currentWaypoint = 0;
} else {
p.Release (this);
Debug.Log ("Oh noes, the target was not reachable: "+p.errorLog);
}
}
public void Update () {
if (Time.time - lastRepath > repathRate && seeker.IsDone()) {
lastRepath = Time.time+ Random.value*repathRate*0.5f;
seeker.StartPath (transform.position,targetPosition, OnPathComplete);
}
if (path == null) {
return;
}
if (currentWaypoint > path.vectorPath.Count) return;
if (currentWaypoint == path.vectorPath.Count) {
Debug.Log ("End Of Path Reached");
currentWaypoint++;
return;
}
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
dir *= speed;
controller.SimpleMove (dir);
if ( (transform.position-path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance*nextWaypointDistance) {
currentWaypoint++;
return;
}
}
}