Using Lerp with rotation is useful for rotating an object to an angle over a period of time, or finding a weighted rotation between two others.
Just like the Vector 3 or Vector 2 Lerp functions using Lerp with rotation involves passing a starting value, a target value, and progression value, T, to interpolate between the two over time.
This can be done using Quaternion rotation values, which are the native rotation values that Unity uses.
Like this:
public Vector3 targetRotation;
void Start()
{
StartCoroutine(LerpFunction(Quaternion.Euler(targetRotation), 5));
}
IEnumerator LerpFunction(Quaternion endValue, float duration)
{
float time = 0;
Quaternion startValue = transform.rotation;
while (time < duration)
{
transform.rotation = Quaternion.Lerp(startValue, endValue, time / duration);
time += Time.deltaTime;
yield return null;
}
transform.rotation = endValue;
}
See also: