0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You can use _object1.transform.rotation.eulerAngles.z and _object2.transform.rotation.eulerAngles.z and then you can use transform.rotation = Quaternion.EulerRotation(new Vector3(x,y,z));

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The problem with that is while the knob/boxA and the hand/boxB start relatively aligned, they're both nested under several objects that have rotations (which also aren't guaranteed/consistent). So converting to euler won't work because unity is not consistent with how rotation (quaternions) to euler is presented, ie because of parent rotations, any rotation.euler.z could be lost/absorbed into other axis'. \$\endgroup\$
    – ThatOneGuy
    Feb 22 at 21:39
  • 1
    \$\begingroup\$ I don't recommend using Euler angles for any purpose where you need to use them as inputs for math. This isn't a Unity thing, it's the fact that Euler/Tait-Bryan angles do not represent orientation in a uniform way, and introduce wrap-around problems that are easier to sort in pure vector or quaternion approaches. \$\endgroup\$
    – DMGregory
    Feb 22 at 21:52
  • \$\begingroup\$ For me it is hard to understand what you want to achieve, it sounds impossible (or requires very complex math). If BoxA is a child of knob and boxB is child of hand you cannot compare the global rotation of these objects because their parent rotations make them uncomparable. Because they belong to different coordinate spaces with their own rotations (knob and hand coordinates). The rotation of knob will change the position of BoxA if BoxA local position is not Vector3.zero etc.. \$\endgroup\$
    – Bogdan
    Feb 23 at 22:10
  • \$\begingroup\$ You can also try to use this method maybe it can help you to achieve what you need: Quaternion.LookRotation(transform.forward, Vector3.up).eulerAngles.z \$\endgroup\$
    – Bogdan
    Feb 23 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .