概述
变换保持关于一个对象的大部分数据,包括它的父,子,位置,旋转和比例。它还具有修改每个属性的功能。每个 GameObject 都有一个 Transform。
翻译(移动)一个物体
// Move an object 10 units in the positive x direction
transform.Translate(10, 0, 0);
// translating with a vector3
vector3 distanceToMove = new Vector3(5, 2, 0);
transform.Translate(distanceToMove);
旋转物体
// Rotate an object 45 degrees about the Y axis
transform.Rotate(0, 45, 0);
// Rotates an object about the axis passing through point (in world coordinates) by angle in degrees
transform.RotateAround(point, axis, angle);
// Rotates on it's place, on the Y axis, with 90 degrees per second
transform.RotateAround(Vector3.zero, Vector3.up, 90 * Time.deltaTime);
// Rotates an object to make it's forward vector point towards the other object
transform.LookAt(otherTransform);
// Rotates an object to make it's forward vector point towards the given position (in world coordinates)
transform.LookAt(new Vector3(10, 5, 0));
可以在 Unity 文档中看到更多信息和示例。
另请注意,如果游戏使用刚体,则不应直接与变换进行交互(除非刚体具有 isKinematic == true
)。在这些情况下,使用 AddForce 或其他类似方法直接作用于刚体。