A* Pathfinding- A moving target

So, Im using the A* pathfinding project, all is going well, and I’ve created a working AI. Great, however, it only goes to the targets original position, so if the target moves once the game starts, the AI wont notice. I tried adding all the stuff (minus GetComponent(CharacterController)) to the update function, but it doesnt work. Any advice on how I can work this script with moving targets? Thanks

#pragma strict
import Pathfinding;

var turnSpeed : float = 10.0;
var Target : Vector3;
var seeker : Seeker;
var CharCont : CharacterController;
var path : Path;
var speed : float = 100;
var NextWaypointDistance : float = 0.8;
private var currentWayPoint : int = 0; 


function Start () {
CharCont = GetComponent(CharacterController);
Target = GameObject.FindWithTag("Target").transform.position;
GetNewPath();

}

function Update () {
}

function GetNewPath(){
seeker.StartPath(transform.position, Target, OnPathComplete);
}

function OnPathComplete(newPath : Path){
if (!newPath.error){
path = newPath;
currentWayPoint = 0;
}
}

function FixedUpdate(){

if (path == null){
return;
}
if (currentWayPoint >= path.vectorPath.Count){	//This is when its reached the end of its path
return;
}

var dir : Vector3 = (path.vectorPath[currentWayPoint]-transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;

CharCont.SimpleMove(dir);

if (Vector3.Distance(transform.position, path.vectorPath[currentWayPoint]) < NextWaypointDistance){
currentWayPoint++;
}


}

Well a sure thing that is working to put your GetNewPath function in Update().
However you can start thinking on some optimizations like only search for new path when the target changed position. Or if the target is not moving too fast you could start for a new path in every 0.5 seconds. There is a script in the project called AIPath. It is dealing with a lot of stuff including with a timer that determines the time between path searches. If you like it you can use it as well by making a class that is derived from this class or you can just copy this part of the code :slight_smile:

The optimization depends on your game logic. If the target is constantly moving it is better to keep search every x seconds. But if your target only move sometimes it is better to search every time it changes position :slight_smile: