[SOLVED]Quaternion lerp rotation

Hey,
So I am trying to make my object rotate naturally like any bike crank would… DataShow.data0 is the angle i am getting from my pedal. Now if I put value like 200 in there it works as intended but if I leave this data0 variable there ist doesnt rotate slowly to that angle but just jumps to it. I tryied printing values of data0 and they are from -200 to 200, and putting a value in that range insted of variable works. What am i missing?

void FixedUpdate () {  
        //Debug.Log(DataShow.data0);
        crankLeft.transform.localRotation = Quaternion.Euler(new Vector3(crankLeft.transform.localRotation.x, Mathf.Lerp(crankLeft.transform.localRotation.y, DataShow.data0 , Time.time*1f), crankLeft.transform.localRotation.z ));
    }

Thanks

Edit:
I am getting data0 from script that was loaded in another scene and has dontdestroyonload. So I start getting data, so I have to switch scenes to actually use my code, that got me thinking that Time.time might not be the best so i tryied Time.timeSinceLevelLoad but it did not help.

You should use Slerp, I think. (Spherical linear… something something. ) :slight_smile:
It’s format is basically the same as Lerp, which you’re not using correctly.
Lerp/Slerp are often best used in Coroutines, I find… It’s not necessary, but seems easier.

You used FixedUpdate : do you want to use physics to rotate? That would be different.

Also, I am pretty sure you should use localRotation.eulerAngles.(x/z) instead of localRotation, which also has x,y,z, and ‘w’. for the 4 parts of a Quaternion.

This might be an option, too: Unity - Scripting API: Quaternion.RotateTowards
That would be ‘instead of’ not as well as*

1 Like

Don’t use Lerp with Time.time like that. The third parameter to lerp, t, is not time, it is an interpolation parameter and should be between 0 and 1.

Lerp does linear interpolation between two values a and b, when t = 0, the returned value is equal to a, when t = 1, it is equal to b, and when it is for example 0.5, the value will be halfway between a and b.

Time.time will almost always be greater than 1, and so the value will always be b.

Instead, try Mathf.MoveTowards or Mathf.MoveTowardsAngle, those should work with your current setup.

For a better solution, look into using Quaternion.AngleAxis and Quaternion.RotateTowards.