- Home /
Object rotation to exact degrees not working
Hi all,
I have almost completed my project but have one major problem. I have a 3d cube that I am trying to set the rotation of at the beginning of the game. It will be a different face showing each time the level is loaded. The problem is that I keep getting strange numbers coming up for the x y z degrees.. Instead of it being 0 for x it sometimes comes up with something like -6.5587e or some strange minus figure! This also happens with y & z too, and is only occurs some of the time, but I do need the roation to be an exact multiple of 90 degrees, i.e 0, 90, 180 etc.
I am setting the rotation as in the code below:
// Chosen X Axis var FaceX : int = 0;
// Chosen Y Axis var FaceY : int = 90;
// Chosen Z Axis var FaceZ : int = 270;
Cube_1.transform.eulerAngles.x = FaceX; Cube_1.transform.eulerAngles.y = FaceY; Cube_1.transform.eulerAngles.z = FaceZ;
This does change the face showing correctly, but the inspector window sometimes shows these strange minus numbers instead of 0. I have also tried it as:
Cube_1.transform.eulerAngles = Vector3(FaceX, FaceY, FaceZ);
The same thing happens. Can anyone please suggest anything to solve this???
Answer by syclamoth · May 10, 2012 at 12:14 PM
The reason the euler angles need to be calculated is because Unity does not store rotation information that way internally. This is why they sometimes show strange numbers. It can also be attributed somewhat to floating-point error- these numbers you see are close enough to epsilon that they may as well be zero.
Why do you need the numbers to be so precise? Make sure that you're not asking for something that the game engine can't actually do, and be prepared to use reasonable approximations.
Answer by GameDev2012 · May 10, 2012 at 01:07 PM
Hi, thanks for your answer, that makes sense.
I have a set of 3d cubes all with different coloured faces. They rotate in groups of 3, so they can show a different face than the rest of the cubes. I need the numbers to be exact so that I can tell when all of the cubes are showing the same colour. I'm doing this based on their rotation vales for x y and z. Unless there is another way of being able to detect when all the cubes show the same face?? I'm a bit stuck. Any suggestions welcomed.
Answer by GameDev2012 · May 10, 2012 at 01:07 PM
Hi, thanks for your answer, that makes sense.
I have a board of 3d cubes with different coloured faces. They rotate in groups of 3, so a different coloured face may show from the rest of the board. I need the values to be exact so that I can tell when all of the cubes are showing the same colour. Is there any other way of detecting when all the cubes are showing the same colour to the user?? Or any other way of setting the rotation value to be exact? Any suggestions greatly welcomed.