講解的 Unity 中幾種不同的坐標(biāo)系與其之間的轉(zhuǎn)換,以及匯總物體的移動和旋轉(zhuǎn)方法
本文原地址:Unity學(xué)習(xí)—坐標(biāo)系與空間變換
坐標(biāo)系
坐標(biāo)系種類
Unity 中使用到的坐標(biāo)系分為以下四種
-
世界坐標(biāo)系 Word Space
即世界空間使用的坐標(biāo)系,基本單位 unit,x 正方向:左向右, y 正方向:下向上,z 正方向:屏外向屏內(nèi),
任何物體使用 Transform.position 即可獲得世界坐標(biāo)值
場景中根物體使用的就是世界坐標(biāo),可在 Inspector 查看世界坐標(biāo)值
對于非根物體則以父物體位置為原點(diǎn)位置使用本地坐標(biāo)系 Local Space,即相對父物體位置,該物體 Inspector 數(shù)值為本地坐標(biāo)值,可使用 Transform.localposition 獲取本地坐標(biāo)值
-
屏幕坐標(biāo)系 Screen Space
- 基本單位像素,屏幕左下角為(0,0),右上角為(Screen.width,Screen.height),即實(shí)際運(yùn)行屏幕下的游戲窗口像素值,z 為相機(jī)世界坐標(biāo)單位值
- Input.mousePosition 獲取的鼠標(biāo)坐標(biāo),Input.GetTouch(0).position 獲取觸摸坐標(biāo)
-
視口坐標(biāo)系 Viewport Space
- 左下角為(0,0),右上角為(1,1),z 為相機(jī)世界坐標(biāo)單位值
- 適合用于坐標(biāo)系轉(zhuǎn)換
-
UGUI 坐標(biāo)系 UGUI Space
- 基本單位像素,屏幕左上角為(0,0),右下角為(Screen.width,Screen.height)
坐標(biāo)系轉(zhuǎn)換
// 本地→世界
transform.TransformPoint(position);
// 世界→本地
transform.InverseTransformPoint(position);
// 世界→屏幕
camera.WorldToScreenPoint(position);
// 世界→視口
Camera.main.WorldToViewportPoint(position)
// 屏幕→視口
camera.ScreenToViewportPoint(position);
// 視口→屏幕
camera.ViewportToScreenPoint(position);
// 視口→世界
camera.ViewportToWorldPoint(position);
空間變換
坐標(biāo)系選擇
世界坐標(biāo)系 Space.World 與自身坐標(biāo)系 Space.Self
Vector3.forward、Vector3.back、Vector3.left、Vector3.right、Vector3.up、Vector3.down 數(shù)值固定
transform.forward、 transform.right、transform.up 數(shù)值不定,依據(jù)物體自身旋轉(zhuǎn)變化,如 transform.forward 為物體 z 軸在世界坐標(biāo)系中所指方向
非剛體變換
非剛體物體在移動或旋轉(zhuǎn)時(shí)不檢測碰撞
非剛體移動
Transform.positon 世界位置坐標(biāo)
Transform.localPostion 本地位置坐標(biāo)
Transform.Translate(Vector3 translation, Space relativeTo = Space.Self)
Transform.Translate(float x, float y, float z, Space relativeTo = Space.Self)
Transform.Translate(Vector3 translation, Transform relativeTo)
-
Transform.Translate(float x, float y, float z, Transform relativeTo);
void Update() { // 以每秒1個(gè)單位速度延世界坐標(biāo)系y軸正方向移動 transform.Translate(Vector3.up * Time.deltaTime, Space.World); // 以每秒1個(gè)單位速度延主相機(jī)本地坐標(biāo)系x軸正方向移動 transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform); }
-
Vector3.MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
以一定速度向目標(biāo)移動直至到達(dá)目標(biāo)位置,對比插值法可限制最大速度
// maxDistanceDelta 最大移動距離 void Update() { // 當(dāng)前幀移動距離 float step = speed * Time.deltaTime; // 由當(dāng)前位置向目標(biāo)位置移動距離 step 得出新位置,超過終點(diǎn)則返回終點(diǎn)值 transform.position = Vector3.MoveTowards(transform.position, target.position, step); }
-
Vector3.SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime)
實(shí)現(xiàn)平滑阻尼移動,可控制速度,一般用于攝像機(jī)跟隨
// 通過使用自身修改的速度指針計(jì)算當(dāng)前位置 private Vector3 velocity = Vector3.zero; void Update() { // 定義目標(biāo)物體的后上方為目標(biāo)位置 Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10)); // 以每幀變化的速度 velocity 由當(dāng)前位置向目標(biāo)位置移動 transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 0.3F); }
Vector3.Lerp(Vector3 a, Vector3 b, float t)
Vector3.LerpUnclamped(Vector3 a, Vector3 b, float t)
Vector3.Slerp(Vector3 a, Vector3 b, float t)
-
Vector3.SlerpUnclamped(Vector3 a, Vector3 b, float t)
插值法,適用于已知起點(diǎn)與終點(diǎn)的情況
void Update() { // 當(dāng)前時(shí)間已移動距離 float distCovered = (Time.time - startTime) * speed; // 已移動距離與總距離比例 float fractionOfJourney = distCovered / journeyLength; // 對起點(diǎn)終點(diǎn)之間插值取相應(yīng)比例值,范圍 0.0-1.0 即 startMarker.position - endMarker.position transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney); } void Update() { // 弧心 Vector3 center = (sunrise.position + sunset.position) * 0.5F; // 下移弧心使弧垂直 center -= new Vector3(0, 1, 0); // 對弧插值 Vector3 riseRelCenter = sunrise.position - center; Vector3 setRelCenter = sunset.position - center; // 已過時(shí)長占總時(shí)長比例 float fracComplete = (Time.time - startTime) / journeyTime; transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete); transform.position += center; }
// 線性插值法計(jì)算兩點(diǎn)確定直線任意位置 無范圍限制 Vector3.LerpUnclamped(startPostion, targetPosition, 3.2f); // 球形插值,插值結(jié)果為由兩向量間角度和長度插值得到的向量值,常用于計(jì)算太陽軌跡 Vector3.Slerp(startPostion, targetPosition, 0.3f);
// 數(shù)學(xué)插值 gameObject.transform.localPosition = new Vector3( Mathf.Lerp(startPostion.x, targetPosition.x, MoveSpeed * Time.deltaTime), Mathf.Lerp(startPostion.y, targetPosition.y, MoveSpeed * Time.deltaTime), Mathf.Lerp(startPostion.z, targetPosition.z, MoveSpeed * Time.deltaTime));
非剛體旋轉(zhuǎn)
旋轉(zhuǎn)需使用旋轉(zhuǎn)方法,不要直接修改屬性值,當(dāng)超過360會發(fā)生錯(cuò)誤
Transform.eulerAngles 歐拉角
Transform.localEulerAngles 本地歐拉角
Transform.rotation 四元數(shù)
Transform.Rotate(Vector3 eulers, Space relativeTo = Space.Self)
Transform.Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self)
-
Transform.Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self)
void Update() { // 以物體 y 軸正方向以30°每秒的速度旋轉(zhuǎn) transform.Rotate(Vector3.up * 30 * Time.deltaTime, Space.Self); }
-
Transform.RotateAround(Vector3 point, Vector3 axis, float angle)
void Update() { // 繞世界坐標(biāo)系中目標(biāo)位置的 y 軸正方向以30°每秒旋轉(zhuǎn)和移動(即繞點(diǎn)畫圓) transform.RotateAround(target, Vector3.up, 30 * Time.deltaTime); }
Transform.LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up)
-
Transform.LookAt(Transform target, Vector3 worldUp = Vector3.up)
以y為軸旋轉(zhuǎn),使z軸指向目標(biāo)
// 旋轉(zhuǎn)使物體 z 軸指向目標(biāo)位置,且 x 軸同目標(biāo)方向與 upwards 的叉積方向一致,y 軸同 z 和 x 軸的叉積方向一致 transform.LookAt(targetPosition, Vector3.up); transform.LookAt(target.transform, Vector3.up);
-
Vector3.RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
//maxRadiansDelta 旋轉(zhuǎn)最大弧度差 //maxMagnitudeDelta 旋轉(zhuǎn)最大長度差 void Update() { // 確定旋轉(zhuǎn)方向 Vector3 targetDirection = target.position - transform.position; // 一幀旋轉(zhuǎn)角度 float singleStep = speed * Time.deltaTime; // 將物體 z 軸正方向向目標(biāo)方向旋轉(zhuǎn)一幀角度,得到當(dāng)前幀方向 Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f); // 計(jì)算由 z 軸正方向旋轉(zhuǎn)到當(dāng)前幀旋轉(zhuǎn)方向的四元數(shù)角度 transform.rotation = Quaternion.LookRotation(newDirection); }
-
Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)
void Update() { // 當(dāng)前一幀旋轉(zhuǎn)角度 var step = speed * Time.deltaTime; // 由當(dāng)前旋轉(zhuǎn)角度向目標(biāo)旋轉(zhuǎn)角度旋轉(zhuǎn)一幀角度 transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step); }
-
Quaternion.FromToRotation(Vector3 fromDirection, Vector3 toDirection)
void Start() { // 將物體 y 軸旋轉(zhuǎn)至當(dāng)前 z 軸正方向 transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward); }
-
Quaternion.LookRotation(Vector3 forward, Vector3 upwards = Vector3.up)
若 forward 或 upwards 大小為0則返回0
若 forward 與 upwards 共線則返回0
void Update() { // 目標(biāo)方向 Vector3 relativePos = target.position - transform.position; // 計(jì)算由 z 軸正方向旋轉(zhuǎn)到目標(biāo)方向的角度,且 x 軸同目標(biāo)方向與 upwards 的叉積方向一致,y 軸同 z 和 x 軸的叉積方向一致 Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up); transform.rotation = rotation; }
-
Quaternion.AngleAxis(float angle, Vector3 axis)
void Start() { // 繞世界坐標(biāo)系 y 軸正方向旋轉(zhuǎn)30° transform.rotation = Quaternion.AngleAxis(30, Vector3.up); }
Quaternion.Lerp(Quaternion a, Quaternion b, float t)
Quaternion.LerpUnclamped(Quaternion a, Quaternion b, float t)
Quaternion.Slerp(Quaternion a, Quaternion b, float t)
-
Quaternion.SlerpUnclamped(Quaternion a, Quaternion b, float t)
// 基本同上述 Vector3.Lerp void Update() { transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed); transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, timeCount); timeCount = timeCount + Time.deltaTime; } void Update() { transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, timeCount); timeCount = timeCount + Time.deltaTime; }
剛體變換
剛體運(yùn)動會計(jì)算碰撞,不會發(fā)生碰撞體嵌入問題,開啟 Kinematic 則不受力、碰撞等物理作用,對 Rigidbody 的操作都應(yīng)在 FixUpdate
中進(jìn)行
剛體移動
Rigidbody.position 剛體世界位置坐標(biāo)
Rigidbody.velocity 剛體速度
-
Rigidbody.MovePosition(Vector3 position)
與 Rigidbody.interpolation 設(shè)置共同作用,開啟
interpolation
則剛體插值平滑過渡到目標(biāo)位置,移動時(shí)檢測碰撞,void FixedUpdate() { Rigidbody rb = GetComponent<Rigidbody>(); // 剛體向右移動 rb.MovePosition(transform.position + transform.right * Time.fixedDeltaTime); }
-
AddForce(Vector3 force, ForceMode mode = ForceMode.Force)
作用力參考世界坐標(biāo)系
AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force)
-
AddRelativeForce(Vector3 force, ForceMode mode = ForceMode.Force)
作用力參考本地坐標(biāo)系
AddRelativeForce(float x, float y, float z, ForceMode mode = ForceMode.Force)
-
AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode = ForceMode.Force)
在點(diǎn)上施加力,會給物體加上扭力
-
AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier = 0.0f, ForceMode mode = ForceMode.Force))
void FixedUpdate() { Rigidbody rb = GetComponent<Rigidbody>(); // 物體 z 軸正方向施加1個(gè)單位的力 rb.AddForce(transform.forward * 1.0f); // 物體 z 軸正方向施加1個(gè)單位的沖力 rb.AddForce(0, 0, 1.0f, ForceMode.Impulse); // 物體 z 軸正方向施加1個(gè)單位的力 rb.AddRelativeForce(Vector3.forward * 1.0f); // 模擬爆炸力,由爆炸中心向物體中心施加1個(gè)單位力 Vector3 direction = from.transform.position - transform.position; body.AddForceAtPosition(direction.normalized, transform.position); }
剛體旋轉(zhuǎn)
Rigidbody.rotation 剛體旋轉(zhuǎn)
Rigidbody.angularVelocity 剛體角速度
-
Rigidbody.constraints 剛體約束
約束剛體在某一或多軸的移動或旋轉(zhuǎn)
// 限制剛體在 z 軸的移動和旋轉(zhuǎn) m_Rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ;
-
Rigidbody.MoveRotation(Quaternion rot)
與 Rigidbody.interpolation 設(shè)置共同作用,則剛體插值平滑旋轉(zhuǎn)到目標(biāo)角度
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.deltaTime); m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
Rigidbody.AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
Rigidbody.AddTorque(float x, float y, float z, ForceMode mode = ForceMode.Force)
Rigidbody.AddRelativeTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
-
Rigidbody.AddRelativeTorque(float x, float y, float z, ForceMode mode = ForceMode.Force)
void FixedUpdate() { Rigidbody rb = GetComponent<Rigidbody>(); // 根據(jù)輸入水平數(shù)據(jù)繞物體 y 軸正方向添加扭力 float turn = Input.GetAxis("Horizontal"); rb.AddTorque(transform.up * torque * turn); // 物體 y 軸正方向添加扭力 rb.AddRelativeTorque(Vector3.up * torque * turn); }