# Rotate an object on itself, from one random Quaternion to another

I have a situation: I have a 3D object in the world. let's say a sphere. I have 2 random directionnal vector: vector A, and vectorB:

My question is: How to I rotate over time my object, from A to B ? The vector A is important: I don't want to simply rotate the forward of the object from current forward to B.

I know I can use the function vector3 C = Vector3.SmoothDamp(...) in unity to lerp between my 2 vectors A & B.

but then ok, I have vector3 C, how do I apply the rotationof my object to C ? if don't want to do: gameObject.transform.forward = C;

I want something like: gameObject.transform.rotation = SomeQuaternion(C, initial rotation A). or something.

Thank you for help !

PS: I don't want to parent / unparent gameObject or something like that, i want the math answer, using Quaternion.

Since you have two vectors to begin with, instead of two quaternions, Find the quaternion representing the rotation from one vector to another.

Then you can take a copy of the current rotation quaternion, and multiply it with the quaternion you got from the vectors, which gives you the final rotation quaternion. You can then lerp from your copy of the current rotation quaternion to the final rotation quaternion you computed.

You had the right keyword: lerp.

To be more precise, Quaternion.Lerp.

Example from docs.unity3d.com:


using UnityEngine;

public class Example : MonoBehaviour
{
// Interpolates rotation between the rotations
// of from and to.
// (Choose from and to not to be the same as
// the object you attach this script to)

Transform from;
Transform to;
float speed = 0.1f;
void Update()
{
transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
}
}

• I have one problem with that: if I apply this rotation, at the start of the lerp, the current rotation of my gameObject will "snap" to from.rotation. (because my gameObject is not at the same rotation as from.rotation at start). I would like to apply this rotation, taking into acount the current rotation Dec 11, 2019 at 13:35
• @UgoHed if you have two quaternions A and B, and your initial rotation O, and you want a quaternion C such that C is to O as B is to A... that is rule of three. I'm not sure about doing it Unity. For what I read about Unity Quaternion Multiplication it applies them in inverse order. However, I think you have vectors to begin with, we can do it by other means, I'll update the answer. Dec 11, 2019 at 17:38