How can I get an object to turn on Waypoints

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

Try this:

transform.LookAt(target);

Where target is the next transform you want to go to. You might also want to lookup NavMesh as it maps the terrain to find “walkable” areas.

One way that I do this is to use a NavMesh Agent. It will automatically turn when it gets to its point. Just use SetDestination until it gets close to your waypoint then set the other destination. You could try to include that.