im working on ai for a hovercar game and all is working well exept i need it to find the closest waypoint so it never goes backwards becase at the moment it turns around and goes back to the last waypoint if it dident get within 10m of it? any idears on how to do this? hear is my current script
var waypoints : Transform[];
var speed = 0;
var upforce = 0;
var upforceidle = 0;
private var curwp : int;
function Start()
{
curwp = 0;
}
function Update ()
{
var hit : RaycastHit;
if (Vector3.Distance(waypoints[curwp].position, transform.position)< 10)
{
curwp ++;
}
transform.LookAt(waypoints[curwp]);
rigidbody.AddRelativeForce (Vector3.forward * speed);
if (Physics.Raycast (transform.position, -Vector3.up, hit))
{
if (hit.distance < 1)
{
rigidbody.AddForce (Vector3.up * upforce);
}
else
{
rigidbody.AddForce (Vector3.up * upforceidle);
}
}
}