Can't find why 3rd parameter of Quaternion.Lerp takes so long to reach 1

Hello!

So basically, I’m rotating an object to look at another object. Simple, right? Also, I’ve used both Vector3.Lerp and Quaternion.Lerp many times before, so I have no idea what’s going on here:

[Header("Rotating info")]
    public bool IsRotating;
    public float AmountToTurn;
    public float TotalTime;
    public float StartTime;
    public float RotateSpeed;
    public Vector3 TargetDirection;
    public Quaternion TargetRotation;
    public Transform Target;
    public float LerpNumber;
   
    void Start()
    {
        SetupRotation();
    }

    void FixedUpdate()
    {
        if (IsRotating)
        {
            LerpNumber = (Time.time - StartTime) / TotalTime;
            transform.rotation = Quaternion.Lerp(transform.rotation, TargetRotation, LerpNumber);
        }
    }

    void SetupRotation()
    {
        TargetDirection = Target.position - transform.position;
        TargetRotation = Quaternion.LookRotation(TargetDirection, Vector3.up);
        AmountToTurn = transform.rotation.y - TargetDirection.y;
        if (AmountToTurn < 0)
            AmountToTurn *= -1;
        TotalTime = AmountToTurn / RotateSpeed;
        StartTime = Time.time;
        IsRotating = true;
    }

For some reason, I’m getting an unwanted acceleration and deceleration with Quaternion.Lerp, and it takes a long time for my LerpNumber to reach 1. It reaches 1 long after my object has finished rotating.

What have I missed? Thank you!

You keep using the current rotation as the ‘from’ parameter.

This is what is causing the weird accelleration/deceleration. The distance traversed is shrinking with each update, while the lerp amount is increasing.

If you want a constant speed movement, you should store the start rotation and reuse it with each update.

Or use Quaternion.RotateTowards:

1 Like

Cache the starting rotation and lerp from start to target

1 Like

@lordofduct & @JoshuaMcKenzie Huge thanks! Actually a very silly mistake on my part, I overlooked that each frame I was lerping between angles that kept getting smaller and smaller. All I needed to do was add 2 lines of code and it works as intended:

[Header("Rotating info")]
    public bool IsRotating;
    public float AmountToTurn;
    public float TotalTime;
    public float StartTime;
    public float RotateSpeed;
    public Vector3 TargetDirection;
    public Quaternion StartRotation;
    public Quaternion TargetRotation;
    public Transform Target;
    public float LerpNumber;
   
    void Start()
    {
        SetupRotation();
    }

    void FixedUpdate()
    {
        if (IsRotating)
        {
            LerpNumber = (Time.time - StartTime) / TotalTime;
            transform.rotation = Quaternion.Lerp(StartRotation, TargetRotation, LerpNumber);
        }
    }

    void SetupRotation()
    {
        TargetDirection = Target.position - transform.position;
        TargetRotation = Quaternion.LookRotation(TargetDirection, Vector3.up);
        AmountToTurn = transform.rotation.y - TargetDirection.y;
        if (AmountToTurn < 0)
            AmountToTurn *= -1;
        TotalTime = AmountToTurn / RotateSpeed;
        StartTime = Time.time;
        StartRotation = transform.rotation;
        IsRotating = true;
    }