Euler Angles in code / inspector different (both local & global)
Hey, I've been banging my head against the wall on this for a good while now, googled a bunch with no results. hopefully you can help me.
The Problem
neither the global nor local euler angles for an object (in code), match the euler angles in the inspector. this makes it incredibly difficult to code logic that depends on rotation around a specific axis.
Testing
To test the problem, I reproduced it in neutral conditions,specifically:
used a new object with only one script: that logs the euler angles from code (found below)
object had identity position and scale
euler angles were not manipulated during playmode . they were set via the inspector once then compared to the Debug logs in playmode.
under these conditions, I still got funky discrepancies between the euler angles in the inspector and the code:
I know that modifying euler angles directly is generally discouraged and could lead to gimbal lock, and that euler angle representations aren't unique. But I'd assume the inspector should be in sync with the code on this, otherwise it's massively confusing.
My Goal
What I was originally trying to do is rotate an object on the Z axis (between -45 and +45) from code, by adding torque and using a conditional on torque direction (if euler.z > 45 etc.)
The object I'm trying to rotate has euler (90,0,0) by default, and torque should only affect the Z angles
I'm open to alternatives on how to achieve this, but would like to understand what the hell is going on regardless.
Thanks!
Code for TestRotation:
void Update ()
{
Debug.LogFormat("transform: GLOBAL euler: {0}\t LOCAL euler: {1} \n rotation: GLOBAL euler: {2}\t LOCAL euler {3}",
transform.eulerAngles, transform.localEulerAngles, transform.rotation.eulerAngles, transform.localRotation.eulerAngles);
}
Answer by SuperSpasm · Sep 21, 2017 at 10:33 AM
So it looks like this only happens when euler angles are non-zero on more than one axis. if x,z=0 and I manipulate y only, euler angles are the same in code and in inspector (and behave predictably)
I'm still lost as to why thye're not the same otherwise- any ideas?
Your answer
Follow this Question
Related Questions
I cant find the issue on my code (Quaternion.Slerp) 1 Answer
Rotating transform more than 180 degrees 2 Answers
What is the difference between setting transform.eulerAngles and transform.rotation? 1 Answer
LocalRotation and Transform Rotate 1 Answer
What is the correct way to move objects relative to each other ? 1 Answer