- Home /
Interpolate a rotation with angle axis
Hey there. Suppose there is a sphere currently rotated at random.How can i interpolate the sphere's current rotation so it's Up vector rotates and stays at the worlds up vector(Vector3.up).So i tried doing it like so:
private void Update()
{
transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation * Quaternion.AngleAxis(90, Vector3.up), Time.deltaTime * 1);
}
But that rotates the sphere AROUND the Y axis and does not return it to the world's up vector.With that said i also tried:
private Quaternion mStartingRotation = Quaternion.identity;
private void Start()
{
mStartingRotation = transform.rotation;
}
private void Update()
{
transform.rotation = Quaternion.Lerp(transform.rotation, mStartingRotation * Quaternion.AngleAxis(90, Vector3.up), Time.deltaTime * 1);
}
That also produces the same result as the last code. So what i am basically trying to achieve is on the following to images:
Can somebody help me out
spheres.jpg
(48.8 kB)
Comment
Best Answer
Answer by robertbu · Aug 05, 2014 at 04:15 PM
Assuming I understand what you are asking correctly:
private void Update()
{
Quaternion qTo = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * 1);
}