Answers for "How do you smoothly transition/Lerp into a new rotation?"
http://answers.unity.com/questions/717637/how-do-you-smoothly-transitionlerp-into-a-new-rota.html
The latest answers for the question "How do you smoothly transition/Lerp into a new rotation?"Answer by davidnibi
http://answers.unity.com/answers/1675866/view.html
Quaternion.RotateTowards(<existing rotation>, <new rotation>, ...
So simple! I was doing this through about 5 lines! And I was putting off fixing this for a while!
I've translated between a predefined 'light sway' effect on trees (under normal conditions), to a violent effect in a storm!
{
Quaternion newEulerAngle = Quaternion.Euler(finalAngleX + xRotationOffset, finalAngleY + yRotationOffset, finalAngleZ); //Apply new angle to object
transform.rotation = Quaternion.RotateTowards(transform.rotation, newEulerAngle, turningRate * Time.deltaTime);Sun, 03 Nov 2019 19:21:28 GMTdavidnibiAnswer by rutter
http://answers.unity.com/answers/717653/view.html
The first problem is that [you're using Lerp incorrectly](http://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/).
>Lerp, short for “linear interpolation” does one very simple thing: given two values, x and y, it returns a value that is t percent between them. If you expect the output to change, the arguments you pass in need to reflect that!
>
>It doesn’t make sense to just pass in Time.deltaTime, because that’s only the time that passed during the most recent frame. If your game is running at a constant 50fps, that’s always going to be 0.02.
You need to pass in a value that changes over time.
Suppose we create two variables:
* `lerpTime` is the number of seconds we want the Lerp to take.
* `currentLerpTime` is the number of seconds since we started the Lerp.
When we start the Lerp, we could set `lerpTime` to `5` and `currentLerpTime` to `0`:
lerpTime = 5;
currentLerpTime = 0;
We can increase `currentLerpTime` by `Time.deltaTime` *once per frame*:
//in Update()
currentLerpTime += Time.deltaTime;
Then, we Lerp like this:
Vector3 result = Vector3.Lerp(a, b, currentLerpTime / lerpTime);
Second, if you're able to, you might consider using [Slerp](http://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html), instead. If Euler angles are working for you, go ahead and use them. There are a few corner cases where they have limits, though.Fri, 30 May 2014 00:51:44 GMTrutterAnswer by DMGregory
http://answers.unity.com/answers/717648/view.html
Euler angles are okay for data entry, but lousy for interpolation.
For example, if you wanted to blend between Euler angles (0, 0, 0) and (0, 359, 0), the shortest path is to rotate -1 degrees around the y-axis. But interpolating the two vectors will have your object spin around the long way, +359 degrees.
Euler angles also exhibit [gimbal lock][1] and other interactions between the rotation axes, so when you try to interpolate rotation on multiple axes at once the object can appear to lurch and tumble from one rotation to another, instead of performing one smooth continuous rotation. [Vector3.Slerp][2] won't help here, at least not directly, because it's for interpolating vectors representing spatial directions, not Euler angles.
[Quaternion][3] methods solve all of this automatically, because they don't experience gimbal lock or 0/360 wrap-around issues.
![Quaternions vs Euler][4]
Here you can see the same rotation interpolated with Quaternions (left, smooth & predictable) and Euler angles (right, object tumbles along its long axis during the turn) ([Source][5] and [Interactive Demo][6])
Working with quaternions in Unity is easy thanks to convenience methods like [Quaternion.RotateTowards][7], which you can use something like this:
// Maximum turn rate in degrees per second.
public float turningRate = 30f;
// Rotation we should blend towards.
private Quaternion _targetRotation = Quaternion.identity;
// Call this when you want to turn the object smoothly.
public void SetBlendedEulerAngles(Vector3 angles)
{
_targetRotation = Quaternion.Euler(angles);
}
private void Update()
{
// Turn towards our target rotation.
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
}
This will give you a smooth rotation toward your target orientation, from any starting orientation, at a constant rate of rotation. Note that you can still provide your target orientation using Euler angles, and we can convert that input to an intermediate quaternion representation with one line.
[1]: http://en.wikipedia.org/wiki/Gimbal_lock
[2]: http://docs.unity3d.com/ScriptReference/Vector3.Slerp.html
[3]: http://docs.unity3d.com/ScriptReference/Quaternion.html
[4]: http://habrastorage.org/storage2/bb8/eb5/ea1/bb8eb5ea12f085a8f065281ee222a4a4.gif
[5]: http://sysmagazine.com/posts/183908/
[6]: http://basmanovdaniil.github.io/Quaternions/
[7]: http://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.htmlFri, 30 May 2014 00:46:44 GMTDMGregory