Lerp transf.up towards direction WITHOUT gimbal lock?

I have two non-related transforms and wish to smoothly rotate one of them depending on the orientation of the other.

This can be done with the following code:

_transfA.rotation =   Quaternion.Slerp(_transfA.rotation, _transfB.rotation,  Time.deltaTime*0.01);

However, I now wish to only rotate so that transfA.up eventually reaches transfB.up
And I wish to do that without gimbal lock. That is, when the up direction points completely down, I don’t want the transformA to start spinning around that axis. I presume it therefore shouldn’t use euler angles, but rather some Quaternion trickery.

It’s for a game where I have a spherical planet, and need to orient camera relative to the surface, without the camera going crazy at the north and south pole of the planet.

How to do it?

I think you just want to use the .LookAt() mechanism of a transform, but supply the second argument that is the “up” vector to consider for that point on the planet.

Fortunately for spheres, the “up” vector at any point is simply the normalized position vector!

I think Quaternion.FromToRotation can calculate the rotation necessary to align one given direction with another.

1 Like

Correct, that solves it!

Quaternion wantedRot = Quaternion.FromToRotation( _transfA.up, _transfB.up );

_transfA.rotation =  Quaternion.Lerp(_transfA.rotation,
                                      _transfA.rotation*wantedRot,
                                      Time.deltaTime );

Thanks m8