- Home /
Physics madness: 270º != 270º - Please help
I've been developing a game in Unity for almost 3 years and I'm still trying to understand how to deal with rotations. One very specific situation is breaking my code and I need help on how to fix that. Here's my case scenario:
I have an object which can be rotated to absolute values (0 - 90 - 180 - 270), and I need to check that initial rotation before applying some other transformations. I do this:
int angleZ = (int) m_spriteTransform.rotation.eulerAngles.z;
if(angleZ == 270)
Do some stuff;
else if(angleZ > 180 && angleZ < 270)
Do other stuff;
I need to do that check for reasons that are not important now, but here's what happens. I'm forcing its rotation to be 270 degrees
int angleX = 0;
int angleY = 0;
int angleZ = 270;
m_spriteTransform.rotation = Quaternion.Euler(angleX, angleY, angleZ);
And there is NOTHING at all changing that object's rotation. Anything. However, when I do the first check, the code if(angleZ == 270)
returns FALSE, and the code else if(angleZ > 180 && angleZ < 270)
returns TRUE, which makes no goddam sense.
I'm setting breakpoints and also a Debug.Log info log and, after checking both, angleZ returns 270, so I don't know what is happenning.
How can I deal with this properly?
$$anonymous$$y guess is that you have an error in your logic for converting/comparing between ints and floats, but it's hard to tell from isolated code snippets.
Can you post complete simplified code sample that reproduces the problem?
But... angleZ taken from rotation eulerangles maybe different i think (270 and -90) for example.
if you don't need SIGNED angle, you can use Vector3.Angle (between tranform.up and vector3.up?)
http://docs.unity3d.com/ScriptReference/Vector3.Angle.html
otherwise, something like this:
var angle:float = Vector3.Angle(vectorA, vectorB);
var cross:Vector3 = Vector3.Cross(vectorA, vectorB);
if (cross.y < 0) angle = -angle;
Yes - there's lots of potential issues - Euler angles provide a convenient description of any rotation in three-dimensional space using three numbers, but they're not unique. See Gimbal Lock. That's why Unity uses quaternions.
Precondition it to make sure that angle is never out of 0-360 range. Note this only works if the angle is out by <360.
if(angleZ < 0)
{
angleZ = angleZ + 360;
}
else if(angleZ > 360)
{
angleZ = angleZ - 360;
}
if(angleZ == 270)
Do some stuff;
else if(angleZ > 180 && angleZ < 270)
Do other stuff;
Edit : Sorry, I just realised this probably isnt relevant.
Answer by Sir_Everard · Feb 10, 2015 at 08:56 PM
The eulerAngles variable is not what you need to modify as it is just a return value.
http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
There are a number of rotation functions you can perform to rotate an object.
I think the one you would benefit from most is:
transform.Rotate((float)angleX , (float)angleY, (float)angleZ);
http://docs.unity3d.com/ScriptReference/Transform.Rotate.html
Yep, I reworked the code and switched to that. $$anonymous$$uch more precise behaviour and the erratic compare values are gone. Thanks!
By the way, I was doing rotation = Quaternion.Euler(angleX, angleY, angleZ)
, but I got that wrong in the OP.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182958im_/https://answers.unity.com/themes/thub/images/avi.jpg)