I have a waypoint script that allows a gameobject to follow certain waypoints. Now I’ve been trying (to no avail) to get it to turn when it hits a waypoint thats not in front of it but I can’t get it. Here’s my code for my waypoint:
var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
function Awake(){
waypoint[0] = transform;
}
function Update() {
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * speed;
transform.Rotate(Vector3.right * Time.deltaTime);
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
}