Unity向量按照某一點進行旋轉(zhuǎn)
本文實例為大家分享了Unity向量按照某一點進行旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
一、unity的旋轉(zhuǎn)
首先要知道一點就是在Unity的旋轉(zhuǎn)中使用過四元數(shù)進行旋轉(zhuǎn)的,如果對一個物體的rotation直接賦值你會發(fā)現(xiàn)結(jié)果不是你最終想要的結(jié)果,這個時候我們需要去借助Quaternion來進行旋轉(zhuǎn)。
二、向量按照原點進行旋轉(zhuǎn)
用到的Unity內(nèi)置方法Quaternion.AngleAxis(float angle,Vector3 axis)
第一個參數(shù)就是我們需要旋轉(zhuǎn)的角度 angle大于0時是按照順時針的方向進行旋轉(zhuǎn),angle小于0是按照逆時針的方向旋轉(zhuǎn),這里的旋轉(zhuǎn)時按照坐標原點進行的旋轉(zhuǎn)。
第二個參數(shù)是旋轉(zhuǎn)軸,圍繞哪一個坐標軸進行旋轉(zhuǎn)。
注意:使用這個方法時獲得的也是四元數(shù),我們將其轉(zhuǎn)換成向量Vector3是需要乘以自身的坐標(四元數(shù) * 自身向量,如果反過來 自身向量 * 四元數(shù) 在Unity會發(fā)生編譯錯誤,這里需要注意一下)
案例:將Vector3(1,0,1)按照原點旋轉(zhuǎn)45°,90°,180°,270°測試分別用黑、黃、藍、綠顏色表示
代碼如下:
using UnityEngine; [ExecuteInEditMode] public class VectorDirTest : MonoBehaviour { // Update is called once per frame void Update () { Debug.DrawLine(Vector3.left * 5f,Vector3.right * 5f,Color.red); Debug.DrawLine(Vector3.up * 5f,Vector3.down * 5f,Color.green); Debug.DrawLine(Vector3.forward * 5f,Vector3.back * 5f,Color.blue); Vector3 dir = new Vector3(1,0,1); Debug.DrawLine(Vector3.zero,dir,Color.white); Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(45,Vector3.up) * dir,Color.black); Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(90,Vector3.up) * dir,Color.yellow); Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(180,Vector3.up) * dir,Color.blue); Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(270,Vector3.up) * dir,Color.green); } }
三、向量按照指定位置進行旋轉(zhuǎn)
/// <summary> /// 圍繞某點旋轉(zhuǎn)指定角度 /// </summary> /// <param name="position">自身坐標</param> /// <param name="center">旋轉(zhuǎn)中心</param> /// <param name="axis">圍繞旋轉(zhuǎn)軸</param> /// <param name="angle">旋轉(zhuǎn)角度</param> /// <returns></returns> public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) { return Quaternion.AngleAxis(angle, axis) * (position - center) + center; }
案例:將Vector3(1,0,1)按照Vector(2,0,2)旋轉(zhuǎn)45°,90°,180°,270°測試分別用紅、黃、藍、綠顏色表示
代碼如下:
using UnityEngine; [ExecuteInEditMode] public class VectorDirTest : MonoBehaviour { // Update is called once per frame void Update () { Debug.DrawLine(Vector3.left * 5f,Vector3.right * 5f,Color.red); Debug.DrawLine(Vector3.up * 5f,Vector3.down * 5f,Color.green); Debug.DrawLine(Vector3.forward * 5f,Vector3.back * 5f,Color.blue); Vector3 dir = new Vector3(1,0,1); Vector3 point = new Vector3(2,0,2); Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 45), Color.red); Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 90), Color.yellow); Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 180), Color.blue); Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 270), Color.green); Debug.DrawLine(Vector3.zero, dir, Color.black); } /// <summary> /// 圍繞某點旋轉(zhuǎn)指定角度 /// </summary> /// <param name="position">自身坐標</param> /// <param name="center">旋轉(zhuǎn)中心</param> /// <param name="axis">圍繞旋轉(zhuǎn)軸</param> /// <param name="angle">旋轉(zhuǎn)角度</param> /// <returns></returns> public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) { return Quaternion.AngleAxis(angle, axis) * (position - center) + center; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)軟件開機自動啟動的兩種常用方法總結(jié)
這篇文章主要為大家詳細介紹了C#實現(xiàn)軟件開機自動啟動的兩種常用方法,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下2023-07-07C# SendInput 模擬鼠標操作的實現(xiàn)方法
C# SendInput 模擬鼠標操作的實現(xiàn)方法,需要的朋友可以參考一下2013-04-04C#基礎(chǔ)知識系列八const和readonly關(guān)鍵字詳細介紹
這篇文章主要介紹了C#中的const和readonly關(guān)鍵字,有需要的朋友可以參考一下2014-01-01