- simply take the same steps I mentioned before. but instead of using world vector3 up in the look rotations, get the cross product between the from|to Directions
var OrbitAxis = Vector3.Cross(fromDirection,toDirection);
and have the Look rotations use that instead.
Quaternion fromRotation = Quaternion.Inverse(Quaternion.LookRotation(fromDirection,OrbitAxis));
Quaternion targetRotation = Quaternion.LookRotation(toDirection,OrbitAxis) * fromRotation;
What a Cross Product does is that it takes two lines and finds a third line that would run perpendicular of both lines. essentially this means you can use it to find the axis of rotation. be careful though, if the two reference vectors you pass in are parallel the result would be a zero vector (because its an entire plane that would be perpendicular to both lines, which is infinitely many answers). so if the OrbitAxis is zero you should either cancel the rotation ,snap to target rotation, or fall back to a default rotation (but only if currentRotation != targetRotation otherwise you can get some wobbly rotations at the end).
FromToRotation should also produce similar rotations and thus similar results. however requires slightly different implementation from what I wrote before.
you note that this sort of rotation made have the planet appear upsideDown, the may or may not be intended (though it is what should happen in space). of course its also a simple matter to reorient to up as well.
also if you wanted something really fancy you can use this simple SmoothDamp I wrote for Quaternions a while back so that you can give your rotations a little rotational inertia.
public static Quaternion SmoothDamp(Quaternion current,Quaternion to, ref Vector3 forwardVelocity, ref Vector3 upVelocity, float smoothTime)
{
var forward = Vector3.SmoothDamp(current * Vector3.forward, to * Vector3.forward, ref forwardVelocity, smoothTime);
var up = Vector3.SmoothDamp(current * Vector3.up, to * Vector3.up, ref upVelocity, smoothTime);
return Quaternion.LookRotation(forward,up);
}
public static Quaternion SmoothDamp(Quaternion current,Quaternion to, ref Vector3 forwardVelocity, ref Vector3 upVelocity, float smoothTime, float maxSpeed)
{
var forward = Vector3.SmoothDamp(current * Vector3.forward, to * Vector3.forward, ref forwardVelocity, smoothTime, maxSpeed);
var up = Vector3.SmoothDamp(current * Vector3.up, to * Vector3.up, ref upVelocity, smoothTime, maxSpeed);
return Quaternion.LookRotation(forward,up);
}
public static Quaternion SmoothDamp(Quaternion current,Quaternion to, ref Vector3 forwardVelocity, ref Vector3 upVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
var forward = Vector3.SmoothDamp(current * Vector3.forward, to * Vector3.forward, ref forwardVelocity, smoothTime, maxSpeed, deltaTime);
var up = Vector3.SmoothDamp(current * Vector3.up, to * Vector3.up, ref upVelocity, smoothTime, maxSpeed, deltaTime);
return Quaternion.LookRotation(forward,up);
}
its not true rotational inertia, but the code is super simple, fast, and in nearly all use cases its more than sufficient.