r/Unity3D • • Nov 29 '21

Noob Question How do I get these numbers in code? (Highlighted yellow)

Post image
15 Upvotes

20 comments sorted by

13

u/[deleted] Nov 29 '21

transform.eulerAngles to transform.rotation.eulerAngles

All rotations in Unity are quaternions under the hood, it needs to convert it back to Euler which isn't always going to match the inspector but the rotations are the same even if the values aren't... it's a quaternion thing, they're weird.

1

u/weird_wolfgang Nov 29 '21

That still has the issue of giving me differing values than what the inspector displays. There must be some reference to those yellow numbers yes? I mean they react in real-tie and aren't just read only. There must be some way to reference those numbers in particular yes?

13

u/ChickenHatMan Nov 29 '21

I think you want transform.localeulerangles.

1

u/CSharpBlade Nov 29 '21

Yeah. I think too, because transform.rotation or transform.eulerAngles is global and it's can be reason of different values in code and inspector. Inspector always show local position and rotation.

3

u/[deleted] Nov 29 '21

No, if you could it would be an editor only thing and you couldn't use it in a build. Whatever you're trying to do, you're going about it the wrong way.

Like I said, the rotations are the same, even if the values aren't, you shouldn't rely on those values.

1

u/weird_wolfgang Nov 29 '21

I would think transform.rotation would be it but that doesn't work. I use debug.log to return it and sometimes one will show -9 and the other will be 351. transform.EulerAngles doesn't line up either.

3

u/PuppetPlayVR Nov 29 '21

as far as I know you can't get the exact value seen in the editor. You can only get localEuler values, which are always between 0 and 360.

3

u/Nilloc_Kcirtap Professional Nov 29 '21 edited Nov 29 '21

What are you trying to do with the rotation? There are several ways to get the rotation value but it depends what you plan to do with it.

2

u/shumpfy Professional Nov 29 '21

Transform.localEulerAngles

1

u/DisorderlyBoat Nov 29 '21

What you see in the inspector are local values. This means the rotation relative to its parent. Each parent in the chain up to the top level could affect its rotation, so you won't see the values matching if there is rotation applied to its parent, or any of its parents parents etc... This all applies to position and scale as well.

The inspector also shows rotation as Euler angles as opposed to the under the hood quaternion of how the rotations are implemented.

See this docs reference. It says basically what I said and answers your question:

https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html

1

u/starterpack295 Nov 29 '21

You can also use transform.rotation or transform.localrotation

1

u/PaunitINC Nov 29 '21 edited Nov 29 '21

I have been playing with it and its quite easy.

To SET the rotation of an object you should use:

transform.rotation = Quaternion.Euler(new Vector3(float1, float2, float3);

To get the rotation:

Yourfloat = transform.rotation.youraxis

However it will be in quaternion and not in degrees...

I will search for getting it in degrees for you, however i have to feeling a quaternion is between -1 and 1 ( -180 and 180 degrees) i am not sure, il let you know!

2

u/[deleted] Nov 29 '21

[removed] — view removed comment

1

u/PaunitINC Nov 29 '21

Yes you are right... i ment rotation 😂 il edit it real quick

0

u/VartixGaming Nov 29 '21

Been a while since i've used Unity, but does Transform.localRotation.x or something along those lines work? I remember a long time ago where i tried the same and had to specify for x,y and z after the .transform so maybe either Transform.rotation.x or localRotation might work. Not sure, but worth a shot

-6

u/[deleted] Nov 29 '21

Honestly I have no clue, I’ve tried referencing localRotation but that seems to give a value between 1 and 0 opposed to a 360 degree measure. Unity might have some documentation on it.

0

u/[deleted] Nov 29 '21

[deleted]

0

u/[deleted] Nov 29 '21

[deleted]

2

u/[deleted] Nov 29 '21

You shouldn't ever access quaternion components directly as quaternions are 4-dimensional objects and the x,y,z does not map to their euler values. If you want to store the transform's rotation in degrees around each axis, just create a Vector3 and set it equal to transform.eulerAngles or localEulerAngles.

0

u/[deleted] Nov 29 '21

[deleted]

1

u/[deleted] Nov 29 '21

Storing only the x,y,z of a quaternion will not allow you to accurately represent that rotation as you are losing the w component. If you want to store a rotation with 3 variables, use a Vector3 with euler angles. If you want to fully store a quaternion, then you should use a Quaternion. Using an array is poor advice since at that point you might as well use a vector or quaternion. Using individual floats is okay but most of the time it's easier to just use a vector.