[SOLVED] How to get "Rotation" value that is in the inspector?

Is there a direct way to get the value that are in the Rotation field of the inspector?
I need that angle that is in the inspector. for example Transform > Rotation (X)

It can be the local or world but i need the same value.
I try :

float angleX = myTransformGO.rotation.x;

go from 0 to 0.5 and go down with same signe.

float angleX = myTransformGO.rotation.eulerAngles.x;

but the values are not the same. If i go negative go over 270 with no sign.

So is difficult to make the math. I try variants and manual.

Is there a direct way to get the value that are in the Rotation field of the inspector?
Solution:

    [SerializeField]
    float eulerAngX;
    [SerializeField]
    float eulerAngY;
    [SerializeField]
    float eulerAngZ;

    void Update()
    {
        eulerAngX = transform.localEulerAngles.x;
        eulerAngY = transform.localEulerAngles.y;
        eulerAngZ = transform.localEulerAngles.z;
   
        // add the math for your need using if statemnts
        // ...if ( eulerAngX  > 180f) ... code go here...[/B]

        /* Get X    [ ATT: SIMILAR TO THIS  EXAMPLE  ]

        if (  eulerAngY  > 180f)
          {
              if (  eulerAngX  > 256f)
                  xAngle = ( eulerAngX  *-1f) +360f;
              else
                  xAngle= - eulerAngX;
          } else {

              if (  eulerAngX  > 256f)
                  xAngle=  eulerAngX  - 180f;
              else
                  xAngle= (( eulerAngX  * -1f) + 180f) * -1f;
          }
*/

}

Could anybody write an answer about how to calculate it like Unity does?
HERE IS A LINK TO A BEAUTIFUL MANUAL

5 Likes

No. The Inspector numbers are specially tweaked away from the code values, to look nice for humans. They depend on the starting Inspector values. Put (000) in the Inspector and run some code. Then put (360,360,-360), which is the same as 000, and run the same code. The numbers you see will be different.

The code values you get are always 0-360. If you really want to use raw eulerAngle x,y,z, it’s probably best to rely on that. Maybe if you start the Inspector at (180,180,180), you’ll see the same (never tested.)

The longer version of this is near the end of the rotations chapter in taxesforcatses-dot-com, but’s it’s just more examples and details.

4 Likes

I really really need the inspector values for human reasons. :sweat_smile: [VOTE HERE]
I wish that “transform.Rotation” returns the values is in the inspector. With capital “R”.

2989075--222450--upload_2017-3-10_17-46-33.jpg

3 Likes

transform.rotation.x is a complex number. its not a angle and oscillates from -1 to 1. same goes for y,z,and w components
as for converting to +/- 180:

        private static float WrapAngle(float angle)
        {
            angle%=360;
            if(angle >180)
                return angle - 360;

            return angle;
        }

        private static float UnwrapAngle(float angle)
        {
            if(angle >=0)
                return angle;

            angle = -angle%360;

            return 360-angle;
        }

so WrapAngle will convert 270 to -90

29 Likes

So how do I use this 2 functions? I pass them the “transform.rotation.x” oscillation or the Euler angle?

Your best bet is probably writing your own Rotations script using [SerlializeField] with private variables that read the eulerAgles. Update the values using [ExecuteInEditMode] and you will be able to see the corresponding values in the inspector on your Rotations script. They won’t be the same as the values seen in transform, but they should satisfy your human reasons :wink:

3 Likes

You mean by accuracy? In any case I need the value for my game. I’m not an expert but if i use [ExecuteInEditMode] It will be included into the build?

1 Like

2989438--222479--upload_2017-3-11_16-36-39.png

Sort of like this. They might differ by 360, but you can see them update. If you just use these instead of the values shown in the transform component, you can manipulate them for your game using only the localEulerAngles.

[ExecuteInEditMode]
public class EnableDTScript : MonoBehaviour {


    [SerializeField]
    float eulerAngX;
    [SerializeField]
    float eulerAngY;
    [SerializeField]
    float eulerAngZ;


    void Update() {

        eulerAngX = transform.localEulerAngles.x;
        eulerAngY = transform.localEulerAngles.y;
        eulerAngZ = transform.localEulerAngles.z;

    }
}
12 Likes

I do not need to manipulate them. Yes I had multiple manipulations of them. And at the end, I need the angle of incidence that is exactly what is in the inspector!
But looks like there is no direct way to GET the inspector value. After you make a lot of manipulations to an object (for example using look at) the object quaternion has arbitrary orientation. In the case that arbitrary orientation is fix , then is possible to get the angles by using if statement for each quarter of the 360. And making same math to each value you get the value that is in the inspector. What bothers me is that I can look to the value that is ready for me in the inspector but I’m not able to use it in any way. [FEEDBACK VOTE LINK]

Look, rotations are handled by Quaternions. If you’re doing anything with rotations you need to be familiar with at least Quaternion.Euler(Vector3 input) and the fact that the inspector values are just an euler version of the Quaterion that is in reality the real rotation value of some object. There are many euler ways to represent the same exact Quaternion value, so you can have stuff like (0,0,0) also perfectly equal to (360,-360,-360). That applies to eulers as well, but Quats of course hit more niche values and hopefully you see the point.

You can basically get anything you want in euler values by doing Quaterion.Euler(). If you want to manipulate rotations with euler values, do it with Quaterion.Euler(). If you want to use Euler values, manipulate the rotation of a transform with Quaternion.Euler().

Don’t get your hopes up. You’re asking for something that on a technical level is a non-issue because a little understanding of the context reveals that you can easily use the currently exposed values to accomplish what you’re asking.

2 Likes

I don’t know what everyone keeps talking about. The fact that multiple angles can represent the same Quaternion doesn’t matter, its moot and irrelevant to the OP. which angles that get represented in a Vector3 depends on the rotation order specified. and pretty much everything in unity defaults to the same rotation order, the Transform inspector included. and this is why that fact is irrelevant.

The Transform inspector is simply displaying the local euler angles in the order of ZXY. transform.localEulerAngles will return the same values. This is because ZXY is the default order in Unity for any rotation represented as a Vector3. This includes transform.eulerangles, transform.Rotate (which mentions this in its documentation), and Quaternion.eulerAngles.

Alan just wants to read out the same angles that the inspector displays. He doesn’t have to worry about gimble lock or any special properties revolving around Quaternions. Just use transform.localEulerAngles and be done with it.

as said before. transform.rotation is a Quaternion and the Quaternion components (x,y,z,w) are NOT angles they are complex numbers. unless you understand the math behind Quaterions leave these variables alone. Now Quaternions isn’t rocket science, its just an expansion of high school math. Remember imaginary numbers (square root of -1)? Quaternions is just the 3D version of imaginary numbers.Simply put they are not the variables you want.

you will want to use UnwrapAngle(transform.localEulerAngles.x); You’ll get an angle of x that ranges from -180 to +180.

12 Likes

The problem is it doesn’t – eulerAngles and what the Inspector shows are not the same. Of course, they’re the same angle, but not the same numbers.

The Inspector saves your original hand-entered values. Then it takes the real eulerAngles and adjusts them to keep the Inspector as close to those starting values as possible – using either +/-360 or flipping x (the “over the top” problem.) You can see this – enter 1234 into an Inspector rotation slot. Then run transform.rotation=Quaternion.identity;. It will snap to 1080, which is the closest multiple of 360 to your starting 1234.

That’s why the Inspector seems to prefer -180 to 180, since most slots usually start at 0. But if a slot starts at 180, suddenly Unity prefers 0-360 for that one slot.

4 Likes

Thx @Owen-Reynolds for your Re numbers :wink: → Vector and Rotation link . Good place to start.

Thanks All for the great support!

At the end I finish making the all the math for getting what is in the inspector field. My script is different because the GameObject starting rotation is different from 000 (identity). I think my starting rotation is -90 in Y. Here is the script

// drag and drop in the inspector game object to look at
public Transform myGameObjectTransform;
public float xAngle; // the result value in angles

void FixedUpdate()
    {
        // Get X
        if (myGameObjectTransform.eulerAngles.y > 180f)
        {
            if (dragAlphaArrow.eulerAngles.x > 256f)
                xAngle = (myGameObjectTransform.eulerAngles.x *-1f) +360f;
            else
                xAngle= -myGameObjectTransform.eulerAngles.x;
        } else {

            if (myGameObjectTransform.eulerAngles.x > 256f)
                xAngle= myGameObjectTransform.eulerAngles.x  - 180f;
            else
                xAngle= ((myGameObjectTransform.eulerAngles.x * -1f) + 180f) * -1f;
        }
}

I repeat: My starting rotation is -90 in Y. So that is the reason I put strange arbitrary values as for example 256. And is working correctly. I’m open to critics.

I’m an artist, not a coder or an engineer. If you want to check values for debugging I suggest strongly to make something like this, to double check what you are doing.

1 Like

@Eric5h5 Thanks for your comment. I take my time to understand the problem before answering to you. I disagree with your comment.

Because is not just “Transform.eulerAngles” but the values are not the same. If i go negative go over 270 with no sign. And you get positive numbers with no sign, so you need to compare with another point, etc. Also you need to find that in the documentation and since there are a lot of rotation functions it takes time to understand there is no direct way to get the value.

And as an artist looking the inspector the result bothers me that I can´t just use that value that is in there all the time as I do for example for position , scale or other UI transform variables.

Yes probably using Transform > Rotation (X) (with capital R) is a bad idea and creates confusion. But i wish that value to be accessible. Probably there is a better way to call it. Because is not coherent with other components way of getting values. But with a more appropriate name consistent with the inspector can be more useful for dose as me that do want exactly the same value the is in the inspector without losing too much time.

float AOA = myAirplaneTransform.eulerInspectorAngle.x;

or

float AOA = myAirplaneTransform.getInspectorRotation.x;

or a more intelligent way. But Eric, I wish it as a further because I need it. Actually, in my game, I use it in approximately 100 different game-objects.

1 Like

The inspector values don’t exist in anything other than a temporary form used only for display. What is stored on the transform is Transform.rotation, nothing else. You can use Transform.eulerAngles (same as the inspector) to display the rotation in a more familiar form than quaternions. You may have missed this part: “You can temporarily make rotation values like -90, however toggling play mode will reset them to the actual values that you get from Transform.eulerAngles.” You can’t have a “eulerInspectorAngle” because it doesn’t really exist.

–Eric

3 Likes

I don’t really understand this insistence that you need to get the inspector’s values in code. If a certain rotation is mathematically represented by several solutions, then that’s just the nature of math. As far as human readability goes, 0 and 360 are both human readable, but mathematically speaking the end result is the same.

Also let’s not forget that the inspector is a dev tool. Whatever values are in there are irrelevant to the end user since they’ll never see it. I’m not sure placing this kind of requirement on the inspector makes sense.

Let’s say that even if you do get the inspector values in the code. Now what? You still can’t reliably output to the inspector from code. The inspector is just representing a quaternion.

Someone also pointed out that if you absolutely positively must have values matching between the inspector and code, then you’re going to have to do that yourself using a custom drawer.

If you need to get inspector’s values in code, then you need the code I posted earlier. But even that is not enough. You just get only a quarter of the sphere. I was not able to get the other three-quarters. Is not as simple as it appears or my skills are limited.

At the moment using quaternions is out of my comprehension.

I think you should use C# reflection to get the transform inspector rotation value.
it’s just a Vector3Field for temp vector3 value.
so you should get your selected GameObject and get the TransformInspector instance and to get the Temp field (Vector3) in the TransformInspector.

2 Likes
            Vector3 vect3 = Vector3.zero;
            MethodInfo mth = typeof(Transform).GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic);
            PropertyInfo pi = typeof(Transform).GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
            object rotationOrder = null;
            if (pi != null)
            {
                rotationOrder = pi.GetValue(targetTrs, null);
            }
            if (mth != null)
            {
                object retVector3 = mth.Invoke(targetTrs, new object[] { rotationOrder });
                vect3 = (Vector3)retVector3;
                Debug.Log("Get Inspector Euler:" + vect3);
            }

Try this.
Unity3d Use internal GetLocalEulerAngles method to get the tmp vector3 value in TransformInspector.

3 Likes