Constrain rotation to just 1 axis when using Quaternions

Have been testing a couple days now and the following gives the most accurate results for what I am trying to achieve

placePrefab.transform.rotation = Quaternion.FromToRotation(Vector3.forward, zNormal);

However there is often a very slight ‘tilt’ in the end result.
ie I start with a cube that is perfectly parallel to the floor but wrong y rotation.

I end up with a cube that has the correct y rotation (perpendicular to a given wall) but often has a slight tilt in x or z axis. ie no longer perfectly parallel to the floor.

If I could somehow constrain my Quaternion rotation to just the y axis it would eliminate this slight error in the end result.

The only reliable way I know is to track your rotation as a float angle and synthesize new fresh rotations:

transform.rotation = Quaternion.Euler( 0, angle, 0);

Otherwise you might get drift.

1 Like

Thanks!
Further digging pointed me here as well, same as what you suggest.

The main trick is to force the vectors into being flat, like so:

Vector3 flatzn=zNormal;
flatzn.y=0;
... LookRotation( ..., flatzn);

LookRotation’s best option is now a flat spin around the y=axis.

1 Like