0
\$\begingroup\$

The inspector shows certain rotation values for Game Objects.

I would like to work with these instead of the eulerAngles (which are the rotation values that I can access by a script).

To get these rotation values shown by the Inspector, I found out that the eulerAngles of the game object's localRotation could be of help.

I'm not sure if my approach is correct.

Can somebody tell me what I'm actually trying to do here (I guess there is a name for it) and how to do that correctly?

My current approach is this:

private float EulerToRotation(float value)
{
    if (value > 180)
    {
        return value - 360f;
    }
    else if (value < -180)
    {
        return value + 360f;
    }
    else
    {
        return value;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ transform.localEulerAngles returns the euler angles of the local rotation, that is, the rotation relative to the parent object. What is the difference between these angles and what you want? \$\endgroup\$
    – Alex F
    Feb 24 '19 at 22:34
1
\$\begingroup\$

The angles shown in the Unity inspector are Euler angles, relative to the parent's coordinate space. They correspond to:

transform.localEulerAngles

As long as you're using these to represent an orientation, then differences in the exact numbers you get aren't significant. As explained in this answer, there's an infinite number of ways to represent a given orientation as an Euler angle triplet, all equivalent in effect. So adding / subtracting 360 degrees doesn't make a difference to the net orientation that the triplet represents.

If you're in a situation where the exact numeric value of two equivalent Euler angle triplets makes a difference in your application, then you're probably using the wrong rotation representation for that situation.

For instance, if you want to compare whether an orientation matches a reference orientation, don't compare angles: compute the difference as a quaternion, something like this...

bool RotationMatch(Quaternion a, Quaternion b) {
    return Math.Approximately(Quaternion.Angle(a, b), 0f);        
}

Or, without the trig, using the fact that the quaternion dot product is the cosine of the half the angle between the rotations:

bool RotationsMatch(Quaternion a, Quaternion b) {
    return Mathf.Approximately(Mathf.Abs(Quaternion.Dot(a, b)), 1f));
}
\$\endgroup\$
5
  • \$\begingroup\$ Thank you. I'm not sure if we talk about the same. I have added a screenshot to my post. In this screenshot you can see that the localEuler angle relates to the rotation Y value shown be the Inspector. I don't think the Approximately needs to be used to get the value shown by the Inspector. Am I wrong?` \$\endgroup\$
    – tmighty
    Feb 25 '19 at 12:02
  • 1
    \$\begingroup\$ Yes, we're talking about the same thing. As explained above and in the linked answer, the numeric difference is not significant. There are many equivalent ways to write any orientation as Euler angles. The inspector sometimes shows a different but equivalent formulation. It's generally not a good idea to try to do math on an Euler angle value directly. If you need to compute or blend orientations, quaternions almost always give you a more consistent and sensible basis for that work. \$\endgroup\$
    – DMGregory
    Feb 25 '19 at 12:03
  • \$\begingroup\$ I have added a real life sample. Can you tell me how to deal with this? \$\endgroup\$
    – tmighty
    Feb 25 '19 at 23:45
  • \$\begingroup\$ I guess my question should be "Getting the difference of Y between 2 quaternions", but I'm not sure about it. \$\endgroup\$
    – tmighty
    Feb 25 '19 at 23:51
  • \$\begingroup\$ That's a different question than we started with, so you should post this as a new Question. Be sure to include not just the code you're using now, but also description of the behaviour you want it to have. \$\endgroup\$
    – DMGregory
    Feb 25 '19 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.