0
\$\begingroup\$

I want to rotate an object 360 degrees so this is what I did: xRotation = Mathf.Lerp(0, 360, Time.fixedDeltaTime * lerpTime)

and then I used Quaternion.Lerp() to rotate the Object so:

targetRotation = Quaternion.Euler(initialRotation.x + xRotation, initialRotation.y, initialRotation.z);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.fixedDeltaTime * lerpTime);

But it didn't work as expected, the xRotation didn't go from 0 to 360, it just changed from 0 to 9 (instantly), and that's it, it never changed.

This is my full code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuaternionExpirement : MonoBehaviour
{
    
    [SerializeField] [Range(5, 10)] float lerpTime;
    Vector3 initialRotation;
    bool mousePress, readyToRotate;
    float xRotation;


    private void Awake()
    {
        initialRotation = Vector3.zero;
        mousePress = false;
        readyToRotate = false;
        xRotation = 0;
    }


    private void Update()
    {
        mousePress = Input.GetMouseButtonDown(0);
    }

    
    private void FixedUpdate()
    {
        if (mousePress)
        {
            initialRotation = transform.eulerAngles;
            readyToRotate = true;
        }


        if (readyToRotate)
        {
            xRotation = Mathf.Lerp(0, 360, Time.fixedDeltaTime * lerpTime);
            Quaternion targetRotation = Quaternion.Euler(initialRotation.x + xRotation, initialRotation.y, initialRotation.z);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.fixedDeltaTime * lerpTime);
            
            if (xRotation >= 360)
            {
                readyToRotate = false;
            }
        }
    }


}
\$\endgroup\$
1
\$\begingroup\$

It seems you have not really understood what the functions you use actually do. The Time.fixedDeltaTime, as per the documentation, returns the time between each physics update in seconds. This is a fixed value (e.g. for 50 physics updates per second this will be 0.02). Now, lerpTime is also a fixed value in your case from my understanding. This means that in every FixedUpdate() call, the Mathf.Lerp function will return the same value (360 * 0.02 * lerpTime), in your case 9, which means that Time.fixedDeltaTime * lerpTime = 0.025.

In order to fix the problem the Time.fixedDeltaTime * lerpTime must not be a constant value but must be updated each frame in order to start from 0.025 and eventually reach 1 after 40 frames.

float previousInterpolation = 0f; // member variable
FixedUpdate() {
  previousInterpolation = Time.fixedDeltaTime * lerpTime + previousInterpolation;
  xRotation = Mathf.Lerp(0, 360, previousInterpolation);
}
\$\endgroup\$
1
  • \$\begingroup\$ You want to divide by lerpTime and not multiply by it. As I read the question, the rotation should happen over the the duration of lerpTime and not as a speed. \$\endgroup\$
    – Zibelas
    Dec 24 '20 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.