How do I read Local Space (Self) Rotations?

I must say that after days of searching this forums and trying all various

  • transform.Rotate
  • transform.rotation.eulerAngles.z,x,y
  • transform.localRotation

etc etc stuff that I have found in documentation + here on forums is not giving any results… all the time I am getting some weird angles, as I am reading World Rotations and not Local ones…

for my local rotation I use

transform.Rotate(Vector3.forward, Time.deltaTime * 20, Space.Self);

and it works fine… but as I said, reading x,y,z angles of my object is not working as it should…

Any help pls? All i need is how to get (read) Local Space rotations?

p.s.
at one point in time I was pretty sure this should work:

var ang = transform.rotation.eulerAngles.z;
print(ang);

but nope… not working…

‘Not working’ is a bit broad, so I’ll just guess that you mean the function is returning values you don’t expect to see. In that case it might help to know what you do expect to see. For example, if you rotate the object like you said:

transform.Rotate(Vector3.forward, Time.deltaTime * 20, Space.Self);

and then print out transform.rotation.eulerAngles and expect to see a nice increasing value on one axis and zeros on the others, then you’ll likely not get that unless the transform was aligned to worldspace in the first place.

The “Space.Self” parameter just means that the Rotate function should interpret “Vector3.forward” relative to the object and not the world. So just because the rotation is in “Space.Self” doesn’t change the fact that you’re rotating the object, i.e. changing the transform.rotation, in world space (assuming its a root object and not a child).

It may be that you want to know how much the object has rotated locally from where it started. In that case, store it at the beginning, and then calculate the difference.

e.g. (untested C# to give an idea)

Quaternion startingRotation;

void Start()
{
  startingRotation = transform.rotation;
}

void Update()
{
  transform.Rotate(Vector3.forward, Time.deltaTime * 20, Space.Self);
  Quaternion change = Quaternion.FromToRotation( startingRotation, transform.rotation );
  Debug.Log("Angle changed this much: " + change.eulerAngles);
}

Hopefully that helps.

–Eric

thank you guys ill try what you have proposed

and so I did and nope not getting what I want to get… numbers still running wild…

Molix I have used your idea (needed little tweaking…)

change = Quaternion.FromToRotation(startingRotation.eulerAngles, transform.rotation.eulerAngles);
Debug.Log("Angle changed this much: " + change.eulerAngles);

but in vain… results are not what I need. Again jerky numbers…

Eric5h5 - no, no and no…

Any more ideas? I mean should not this be basic stuff?

… my bad E, it is yes , yes and yes :smile:

used x instead of z side…

it works! Tx!

… well… it seems it is not working as advertised…

if I want to read local space angels in z plane and rotate my object also in y plane I get jerky numbers again… for example z numbers jump from 15 to 190…

so nope localEulerAnles are not what I need here…

You could manipulate the rotation quaternion manually, maybe. Leave W alone (always set to 1), but try setting X, Y and Z yourself.

Note that those properties work with radians, not degrees. So you need to convert your degrees to radians by multiplying them by Mathf.deg2rad.

Let me know if that works, I’m quite curious myself.

tx ill try that…but mind that I don’t want so set anything :slight_smile:

I just want to know how much degrees( or radians - as you said it is easy to convert them) I have bank my object in local space… I mean as I said this should be implemented in API in some obvious way :)))

I use rigid body and AddForce to manipulate with my object quite well… and now I just want to know when my object is 14 degrees banked left or right… (ofc my object is not standing still in world coordinates but moving and turning around… but regarding local rotations that should not matter… (rigt?)

Here is my problem in little more detail

Fist I use this for debugging

Debug.Log("LOCAL EULER Angle changed this much: " + transform.localEulerAngles);
Debug.Log("Angle changed this much: " + transform.eulerAngles);

my idea was to get local and world angles(but I you will see the numbers I get are the same both for local and global euler angles)

while my plane is just rotating in z axis everything seems to be fine

and lets leave it at z=22 degrees…
(and you may notice that transform.localEulerAngler and transform.eulerAngles are the same all the time…(am I doing something wrong here?))

and use rigidbody.AddTorque(0, 0, g * 6); to rotate my plane… so…

and… z keeps changing… (nooooo… whyyyy whyyyyyyyyyyyyyyyyyyyyyy)

…help?

Because you’re converting quaternions to euler angles; you can find lots of info about this with a search (it’s come up more than once :wink: ). Generally in cases like this, you should probably track rotations yourself.

–Eric

What Eric said is true, you can’t work with the quaternion’s converted euler angles, you have to do things manually. Pythagoras wants to be your friend. :slight_smile:

thank you guys but no dice.

i made a invisible copy of my plane and use it just for rotations :wink:

and voila…