Unity學(xué)習(xí)—坐標(biāo)系與空間變換

講解的 Unity 中幾種不同的坐標(biāo)系與其之間的轉(zhuǎn)換,以及匯總物體的移動和旋轉(zhuǎn)方法

本文原地址:Unity學(xué)習(xí)—坐標(biāo)系與空間變換

坐標(biāo)系

坐標(biāo)系種類

Unity 中使用到的坐標(biāo)系分為以下四種

  1. 世界坐標(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)值

  2. 屏幕坐標(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)
  3. 視口坐標(biāo)系 Viewport Space

    • 左下角為(0,0),右上角為(1,1),z 為相機(jī)世界坐標(biāo)單位值
    • 適合用于坐標(biāo)系轉(zhuǎn)換
  4. 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í)不檢測碰撞

非剛體移動

  1. Transform.positon 世界位置坐標(biāo)

  2. Transform.localPostion 本地位置坐標(biāo)

  3. Transform.Translate(Vector3 translation, Space relativeTo = Space.Self)

  4. Transform.Translate(float x, float y, float z, Space relativeTo = Space.Self)

  5. Transform.Translate(Vector3 translation, Transform relativeTo)

  6. 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);
    }
    
  7. 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);
    }
    
  8. 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);
    }
    
  9. Vector3.Lerp(Vector3 a, Vector3 b, float t)

  10. Vector3.LerpUnclamped(Vector3 a, Vector3 b, float t)

  11. Vector3.Slerp(Vector3 a, Vector3 b, float t)

  12. 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ò)誤

  1. Transform.eulerAngles 歐拉角

  2. Transform.localEulerAngles 本地歐拉角

  3. Transform.rotation 四元數(shù)

  4. Transform.Rotate(Vector3 eulers, Space relativeTo = Space.Self)

  5. Transform.Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self)

  6. 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);
    }
    
  7. 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);
    }
    
  8. Transform.LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up)

  9. 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);
    
  10. 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);
    }
    
  11. 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);
    }
    
  12. Quaternion.FromToRotation(Vector3 fromDirection, Vector3 toDirection)

    void Start()
    {
        // 將物體 y 軸旋轉(zhuǎn)至當(dāng)前 z 軸正方向
        transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);
    }
    
  13. 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;
    }
    
  14. Quaternion.AngleAxis(float angle, Vector3 axis)

    void Start()
    {
        // 繞世界坐標(biāo)系 y 軸正方向旋轉(zhuǎn)30°
        transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
    }
    
  15. Quaternion.Lerp(Quaternion a, Quaternion b, float t)

  16. Quaternion.LerpUnclamped(Quaternion a, Quaternion b, float t)

  17. Quaternion.Slerp(Quaternion a, Quaternion b, float t)

  18. 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)行

剛體移動

  1. Rigidbody.position 剛體世界位置坐標(biāo)

  2. Rigidbody.velocity 剛體速度

  3. 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);
    }
    
  4. AddForce(Vector3 force, ForceMode mode = ForceMode.Force)

    作用力參考世界坐標(biāo)系

  5. AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force)

  6. AddRelativeForce(Vector3 force, ForceMode mode = ForceMode.Force)

    作用力參考本地坐標(biāo)系

  7. AddRelativeForce(float x, float y, float z, ForceMode mode = ForceMode.Force)

  8. AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode = ForceMode.Force)

    在點(diǎn)上施加力,會給物體加上扭力

  9. 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)

  1. Rigidbody.rotation 剛體旋轉(zhuǎn)

  2. Rigidbody.angularVelocity 剛體角速度

  3. Rigidbody.constraints 剛體約束

    約束剛體在某一或多軸的移動或旋轉(zhuǎn)

    // 限制剛體在 z 軸的移動和旋轉(zhuǎn)
    m_Rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ;
    
  4. 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);
    
  5. Rigidbody.AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force)

  6. Rigidbody.AddTorque(float x, float y, float z, ForceMode mode = ForceMode.Force)

  7. Rigidbody.AddRelativeTorque(Vector3 torque, ForceMode mode = ForceMode.Force)

  8. 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);
    }
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,996評論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內(nèi)容