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++;
}
}