Transform
Transform 是每个 GameObject 上不可移除的必需 Component,掌管 Position、Rotation 与 Scale,并决定对象在 Scene 层级中的位置。它维护 World 与 Local Space;带 Rigidbody 时应走物理 API。理解父子层级是 Unity 场景搭建基础。
想象一下...
Transform 就像对象的地址卡和身份证。它记录我在哪里(Position)、朝哪边(Rotation)、有多大(Scale)。没有 Transform,Unity 就不知道该把这个对象画在屏幕的什么位置。
概念详解
Transform 在两套坐标系中存数据:World Space
(相对 Scene 原点的绝对坐标)和 Local Space(相对
Parent)。transform.position 是 World;transform.localPosition 是 Local。
旋转在内部以 Quaternion 存储——这样可避免
万向节死锁(gimbal lock)。日常多用 transform.eulerAngles(x、y、z 角度)。要平滑旋转,
可用 Quaternion.Slerp() 或 transform.Rotate()。
性能提示:对有物理的对象(Rigidbody)直接赋值
transform.position 会绕过 Physics Engine,可能穿墙。
应改用 Rigidbody.MovePosition() 或 AddForce()。
World 与 Local 空间
Position
Vector3(x, y, z)
Rotation
Quaternion / euler
Scale
Vector3(1, 1, 1)
动手步骤
用代码移动
transform.Translate(),或赋值 transform.position = new Vector3(...)
平滑旋转
transform.Rotate(Vector3.up * speed * Time.deltaTime)
插值位置(Lerp)
Vector3.Lerp(from, to, t) 或 Vector3.MoveTowards()
朝向目标
transform.LookAt(target),或使用 Quaternion.LookRotation()
交互模拟器
拖动滑块改变 Position、Rotation 和 Scale——画布会实时更新。
Inspector
🟢 绿箭头 = Forward
🔴 红箭头 = Right
🟡 黄点 = 原点 (0,0,0)
代码示例
基础using UnityEngine;
public class TransformBasics : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float rotateSpeed = 90f; // degrees per second
void Start()
{
// Set absolute World Space position
transform.position = new Vector3(0f, 1f, 0f);
// Rotate 45° around the Y axis
transform.eulerAngles = new Vector3(0f, 45f, 0f);
transform.localScale = Vector3.one; // (1,1,1) = original size
}
void Update()
{
// Move forward — multiply by Time.deltaTime so speed is FPS-independent
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
// Spin continuously around Y
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
}
}代码示例
进阶using UnityEngine;
public class TransformAdvanced : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private float followSpeed = 3f;
[SerializeField] private float rotateSpeed = 5f;
void Update()
{
if (target == null) return;
// Smoothly approach the target without overshooting
transform.position = Vector3.MoveTowards(
transform.position,
target.position,
followSpeed * Time.deltaTime
);
// Smoothly look at the target with Slerp
Vector3 dir = (target.position - transform.position).normalized;
if (dir != Vector3.zero)
{
Quaternion lookRot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(
transform.rotation, lookRot,
rotateSpeed * Time.deltaTime
);
}
// Convert World Position → this object's Local Space
Vector3 localPos = transform.InverseTransformPoint(target.position);
// localPos.x > 0: the target is to the right
}
}📌 快速记忆
- ▸Transform 是必需的——任何 GameObject 都无法移除它
- ▸
.position= World,.localPosition= Local - ▸旋转以 Quaternion 存储;日常多改
.eulerAngles - ▸务必 ×
Time.deltaTime,让移动与帧率无关
⚠️ 常见错误
❌ 忘记乘以 Time.deltaTime
120 FPS 的速度是 60 FPS 的两倍
✅ 在 Update() 里始终 × Time.deltaTime
❌ 对带 Rigidbody 的对象直接改 transform.position
绕过物理——穿墙、碰撞异常
✅ 使用 Rigidbody.MovePosition() 或 AddForce()