# Unity use transform.Rotate 3-D rotation angle deviation

2020-11-09 23:48:26

# Unity Use transform.Rotate Three dimensional rotation angle deviation

### situation

In the beginning, it was a complicated situation , I wrote another example . The situation is as follows ：

Mount scripts on a cube ：

``````transform.Rotate(25, 45, 30);
``````

The effect is quite normal ： Change the code ：

``````transform.Rotate(-25, 0, 0);
transform.Rotate(0, -45, 0);
transform.Rotate(0, 0, -30);
``````

It turned out to be ： ### reason

transform.Rotate(x, y, z) Method It's not adding... To the three rotation angles of the object x, y, z, It's based on the three angles we've given , Find a suitable shaft , The corresponding angle of rotation . So when you rotate it three times , The third rotation will affect the angle that has been turned out the first two times .（ Maybe it's just that turning a crooked pen doesn't rotate along the center axis ）

### resolvent

Don't spin apart , Unless it doesn't matter what the angle is . If you want to add or subtract a specified number from the three rotation angles of an object , have access to

`transform.localEulerAngles = new Vector3(transform.localEulerAngles.x + dx, transform.localEulerAngles.y + dy, transform.localEulerAngles + dz);`

transform.localEulerAngles You can't set a single value , It has to be the whole thing Vector3 Pass in .

``````transform.Rotate(-25, 0, 0, Space.World);
transform.Rotate(0, -45, 0, Space.World);
transform.Rotate(0, 0, -30, Space.Self);
``````

I tried , It can be transferred out （-25, -45, -30） The angle of . If you want to make sure that an object rotates in one direction and is separated from the rest ,（ For example, people turn around not to be affected by the body tilt ） It's also a feasible way .