- Home /
How do I make an object look at a point it is moving toward?
I have this driving game and I want to put other cars autonomously driving around on the roads. I have another car and it moves towards waypoints and that works fine, but I cant get it to face the waypoints it is traveling toward. Can anyone help? This is the code I have so far. What changes should I make?
// array to hold waypoint locations var waypoints : Transform[];
// variable to control time taken to travel between points var duration : float = 1.0;
private var startPoint : Vector3; private var endPoint : Vector3; private var startTime : float;
// the array index number of the current target waypoint private var targetwaypoint : int;
function Start() {
 startPoint = transform.position;
 startTime = Time.time;
 if(waypoints.Length <= 0){
     Debug.Log("No waypoints found");
     enabled = false;
 }
 targetwaypoint = 0;
 endPoint = waypoints[targetwaypoint].position;
 
}
function Update () {
 var i = (Time.time - startTime) / duration;
 transform.position = Vector3.Lerp(startPoint, endPoint, i);
 
 
 if(i >= 1){
     startTime = Time.time;
     // increment and wrap the target waypoint index
     targetwaypoint++;
     targetwaypoint = targetwaypoint % waypoints.Length;
     // assign the new lerp waypoints
     startPoint = endPoint;
     endPoint = waypoints[targetwaypoint].position;
        
 }
}
Your answer
 
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
Slicing Objects 1 Answer
"E" to grab an object... 3 Answers
Destroy GameObject and play sound 1 Answer
throw / fling object or prefab 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                