這篇文章將為大家詳細講解有關Unity中怎么將向量按照某一點進行旋轉(zhuǎn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創(chuàng)新互聯(lián)專注于牡丹江網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供牡丹江營銷型網(wǎng)站建設,牡丹江網(wǎng)站制作、牡丹江網(wǎng)頁設計、牡丹江網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務,打造牡丹江網(wǎng)絡公司原創(chuàng)品牌,更為您提供牡丹江網(wǎng)站排名全網(wǎng)營銷落地服務。
一、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; } }
關于Unity中怎么將向量按照某一點進行旋轉(zhuǎn)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
標題名稱:Unity中怎么將向量按照某一點進行旋轉(zhuǎn)
文章轉(zhuǎn)載:http://bm7419.com/article6/jdejig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站設計、企業(yè)網(wǎng)站制作、域名注冊、外貿(mào)建站、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)