世界空间中的变换位置。
游戏代码可以访问 position 成员。可以通过设置该值来为 GameObject 设置动画。下面的示例通过更新 position 来使附加的球体反弹。反弹将慢慢结束。position 也可用于确定变换在 3D 空间中的位置。
using UnityEngine;
// Use Transform.position to bounce a sphere. // The sphere and a quad are colored using materials.
public class ExampleScript : MonoBehaviour { Vector3 velocity = new Vector3(0.0f, 1.0f, 0.0f); float floorHeight = 0.0f; float sleepThreshold = 0.05f; float gravity = -9.8f;
void Start() { transform.position = new Vector3(0.0f, 1.5f, 0.0f); }
void FixedUpdate() { if (velocity.magnitude > sleepThreshold || transform.position.y > floorHeight) { velocity += new Vector3(0.0f, gravity * Time.fixedDeltaTime, 0.0f); }
transform.position += velocity * Time.fixedDeltaTime; if (transform.position.y <= floorHeight) { transform.position = new Vector3(0.0f, floorHeight, 0.0f); velocity.y = -velocity.y; } } }