FHow could I make it so the object with the script always faces the waypoints?

Hey, so i got this script where the game object will follow the waypoints put down. It’s nice but if the way-point is to the right or left the object will simply slide towards it. I want it to turn towards the object and then go over there.

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

I know in C# it would look like this

using System;
using UnityEngine;
 
public class LookAtTarget : MonoBehaviour
{
public Transform target;
 
void Update()
{
if(target != null)
{
transform.LookAt(target);
}
}
}

But I need it in JS and I don’t have a clue where I would put the script.

You need to put it in Update as well:

function Update()
{
    // begin of Update code
    transform.position = Vector3.Lerp(startPoint, endPoint, i);
    transform.LookAt(waypoints[targetwaypoint]);
    // rest of Update code
}

Of course you can add test for null like in your C# example.

EDIT: corrected script a bit…