Error with Rotating script

var waypoint:Transform[];
var speed : float =5;
private var currentWaypoint : int;
var loop:boolean=true;
var rotationSpeed:float=5;

function FixedUpdate () {
          if (currentWaypoint<waypoint.length){
             var target = waypoint[currentWaypoint].position;
             var moveDirection : Vector3 = target - transform.position;

             var velocity = rigidbody.velocity;

            if(moveDirection.magnitude<1){
            currentWaypoint++;
           }

           else{
           velocity = moveDirection.normalized*speed;
           }
    }
    else{
    if(loop){
     currentWaypoint=0;
    }
        else{
        velocity = Vector3.zero;
        }
}

rigidbody.velocity = velocity;
transform.LookAt(target);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);
}

this is an AI script and the

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

Is supposed to make it rotate smoothly , but its making it spaz left and right and generally doesnt have the effect i want , what am i doing wrong

I've found a Slerp/Lerp is more complex than it originally seems.

First of all, your line `transform.LookAt(target)` will instantly snap your object's Z-axis as looking toward that object with no transition effect. That may explain the first spaz motion.

Now for the Slerp. This is how I do it with a `Mathf.Lerp` function, which should work similarly.

float t = 0.0F;
float hitPoints = 0;
float maxHitPoints = 100;
float buildTime = 30.0f; //Seconds

void Update() { //Or FixedUpdate if you want
    if ( hitPoints < maxHitPoints ){
        t += Time.deltaTime;
        hitPoints = Mathf.Lerp(0, maxHitPoints, t / buildTime);
    }
}

The problem is, with your line:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

Every time the `Quaternion.Slerp` is called, `transform.rotation` is changing. The next time `Quaternion.Slerp` is called, it uses a different `transform.rotation` to calculate with.

You want to try and eliminate the variables that change during the calculation if you want a smooth, linear transition.

Basically i found the problem

transform.LookAt(target);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

i had both a LookAt and the Slerp with the other LookAt , so i removed the first line and its ok also it went backwards but i found the problem. it was (transform.position-target) while it should be (target-transform.position)