- Home /
How do I determine which Euler Axis of an object will change when the object is rotated?
I have some art assets that are unfortunately not aligned to Unity's normal axis guidelines (e.g. Y = Up, Z = forward, etc).
Within these are doors that rotate with a HingeJoint. They have a Hinge axis of (-1, 0, 0) which causes them to rotate along Unity's Global Up axis, as desired.
However, when the doors rotate, the values that change are in their Y axis (from 0 decreasing to 270) instead of their X axis (aren't they rotating around the X axis if the Hinge axis is the X axis?).
You can see the hinge faintly near the wall, pointing upwards.
The issue is that these hinge doors are opened using a script that adds force until it detects the door as being at the opened position. It calculates the end position to compare against by adding the hingejoint limit to the resting open position.
e.g.
if (currentRotation == endRotation)
Cease();
where
currentRotation = transform.localEulerAngles.z
and
endRotation = closedRotation + limits;
//There is code in place to determine whether to add the .min or .max and
//to ensure all the euler angles are positive.
The problem is, how do I determine which axis to get the current rotation from? Is there a way to determine which coordinate that will change?
Answer by AlwaysSunny · Jun 14, 2015 at 10:09 PM
One thing, before I forget, never check for equality in the realm of floating point numbers. Check for approximations.
You're making this more complicated than it should be.
Foremost, you almost always can and should avoid futzing with euler representations of rotation. They're handy for measurement and in certain situations, but until you learn when and where they are handy, they'll get in the way of writing strong, clean code. Instead, leverage the extensions in the Vector3 and Quaternion classes. There are many tricks and techniques that take advantage of some vector math principles that would be quite complicated to address using basic trig or euler representations. Unfortunately these are best learned through firsthand exposure; definitely read over the documentation for both classes (Vector3, Quaternion).
Regarding your specific issue, it's a bit too confusing to convey, both on your end and mine. Frankly it seems downright irresponsible to spend any time working through this problem when the real answer is "bypass it altogether". Bear in mind the concepts of local space versus global space, and always pick the strongest way to represent and work with your data. The point is that your doors shouldn't care about their orientation in global space. You define a hinge joint and say this hinge value is fully-closed, these are fully-open, and write this logic in such a way that the door's orientation or rotation value on a specific axis just doesn't matter.
Best,
Your answer
Follow this Question
Related Questions
Is there a 2d joint for a torsion (rotation) spring? 0 Answers
Follow a curve using physics or transform manipulation 2 Answers
How to get rotation relative to the ground normal? 0 Answers
Turning a rigidbody that's in motion 2 Answers
Game Object rotation sometimes doesn't match defined quaternion 0 Answers