# How to get difference in rotation between 2 objects, but only on one axis

Say I have 2 cubes that start with the same rotation. I wish to track the rotation difference between the 2 about the z (forward) axis. Such that if cube A remains stationary and cube B rotates 30 degrees Z, I get 30 degrees. But if it then rotates an additional 30 degrees Y, I get the summation of those 2 rotations (which should be around 42 degrees) on that one axis. But if I only rotate Y or Z, the result is 0.

I'm working within unity and have tried

Quaternion.Angle(boxA.transform.rotation, boxB.transform.rotation, Vector3.forward);

Quaternion.AngleAxis(Quaternion.Angle(boxA.transform.rotation, boxB.transform.rotation), Vector3.forward));


and a custom solution I found online

float angle = 0f;
Vector3 angleAxis = Vector3.zero;
(boxB.transform.rotation * Quaternion.Inverse(boxA.transform.rotation)).ToAngleAxis(out angle, out angleAxis);
return Mathf.DeltaAngle(0f, angle);


All of which will give correct results when only Z is rotated, but gives the angle when any other axis is rotated as well (eg if I only rotate X 45, it spits out 45 despite it not being on the axis plane I'm interested in).

If you need the practical reason I need this info, I'm comparing the rotation between a knob and a hand. Say the knob and hand start with the same rotation, I want to track how far a hand turns and apply it to a knob (under special conditions, so I can't just copy the rotation straight up), I only care about the Z rotation ("forward" into the knob panel); if the hand "rotates" in towards the knob panel, I don't care about that rotation.