使用 Transform.Rotate 以各种方式旋转游戏对象。通常以欧拉角而不是四元数提供旋转。
可以在世界轴或本地轴中指定旋转。
世界轴旋转使用 Scene
的坐标系,因此在您开始旋转 GameObject 时,它的 x、y 和 z 轴与 x、y 和 z 世界轴对齐。所以,如果您在世界空间中随机旋转一个立方体,它的轴就会与世界对齐。在 Unity Editor 的 Scene
视图中选中一个立方体时,将显示左/右、上/下以及正向/反向旋转轴的旋转/辅助图标/。移动这些/辅助图标/将使立方体绕轴旋转。如果您取消选择然后重新选择该立方体,这些轴将重新开始在世界中对齐。
本地旋转使用 GameObject 本身的坐标系。因此,新建的立方体将使用设置为零旋转的 x、y 和 z 轴。旋转该立方体将更新旋转轴。如果您取消选择然后重新选择该立方体,将按之前的方向显示这些轴。\
在本地辅助图标开关中未旋转的立方体\
\
在本地辅助图标开关中旋转的立方体\
\
在全局辅助图标开关中未旋转的立方体\
\
在全局辅助图标开关中旋转的立方体\
eulers | 要应用的旋转。 |
relativeTo | 确定在游戏对象本地还是相对于世界空间中的场景来旋转游戏对象。 |
应用一个围绕 Z 轴旋转 eulerAngles.z 度、围绕 X 轴旋转 eulerAngles.x 度、围绕 Y 轴旋转 eulerAngles.y 度(按此顺序)的旋转。
旋转采用欧拉角形式的 Vector3 参数。第二个参数是旋转轴,可以将其设置为本地轴 (Space.Self) 或全局轴 (Space.World)。旋转以欧拉角度数表示。
relativeTo | 确定在游戏对象本地还是相对于世界空间中的/场景/来旋转游戏对象。 |
xAngle | 围绕 X 轴旋转游戏对象的度数。 |
yAngle | 围绕 Y 轴旋转游戏对象的度数。 |
zAngle | 围绕 Z 轴旋转游戏对象的度数。 |
应用一个围绕 Z 轴旋转 zAngle
度、围绕 X 轴旋转 xAngle
度、围绕 Y 轴旋转 yAngle
度(按此顺序)的旋转。
旋转可以使用 x、y 和 z 的 3 个浮点数指定欧拉角。
该示例显示两个立方体:一个立方体使用 Space.Self(GameObject 的本地空间和轴),而另一个立方体使用 Space.World(相对于 Scene
的空间和轴)。两个立方体的角度相同(使用 3 个 float
)。对两个或三个 float
赋予大约 0.25f 的值时,两个立方体都将旋转。X 值Game
Z 值 Inspector 字段将存储旋转值。立方体按不同方式旋转。若要查看差异,将脚本添加到 Game
,然后在 Game
运行时查看 Scene
视图。在 Scene
视图中,确保 Local
由 Gizmo Toggle
设置,然后选择 Self
立方体。Self
立方体按照 x、y 和 z 箭头方向在立方体面上继续旋转。现在将 Gizmo Toggle
更改为 Global
并选择 World
立方体。这与世界的 x、y 和 z 轴对齐。
using UnityEngine;
// Transform.Rotate example // // Two cubes are created. One (red) is rendered using Space.Self. The // other (green) uses Space.World. The rotation is controlled using xAngle, // yAngle and zAngle. Over time, the cubes rotate differently.
public class ExampleScript : MonoBehaviour { public float xAngle, yAngle, zAngle; public Material selfMat, worldMat;
private GameObject cube1, cube2;
void Awake() { cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube); cube1.transform.position = new Vector3(0.75f, 0.0f, 0.0f); cube1.GetComponent<Renderer>().material = selfMat; cube1.name = "Self";
cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube); cube2.transform.position = new Vector3(-0.75f, 0.0f, 0.0f); cube2.GetComponent<Renderer>().material = worldMat; cube2.name = "World"; }
void Update() { cube1.transform.Rotate(xAngle, yAngle, zAngle, Space.Self); cube2.transform.Rotate(xAngle, yAngle, zAngle, Space.World); } }
angle | 要应用的旋转度数。 |
axis | 要应用旋转的轴。 |
relativeTo | 确定在游戏对象本地还是相对于世界空间中的场景来旋转游戏对象。 |
用给定角度定义的度数围绕给定轴旋转该对象。
旋转具有轴、角度以及本地或全局参数。旋转轴可以为任何方向。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.