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)時按照坐標(biāo)原點進行的旋轉(zhuǎn)。
第二個參數(shù)是旋轉(zhuǎn)軸,圍繞哪一個坐標(biāo)軸進行旋轉(zhuǎn)。
注意:使用這個方法時獲得的也是四元數(shù),我們將其轉(zhuǎn)換成向量Vector3是需要乘以自身的坐標(biāo)(四元數(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">自身坐標(biāo)</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">自身坐標(biāo)</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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Visual C#進行圖像處理(讀取、保存以及對像素的訪問)
本文主要介紹利用C#對圖像進行讀取、保存以及對像素的訪問等操作,介紹的比較簡單,希望對初學(xué)者有所幫助。2016-04-04
Entity?Framework模型優(yōu)先與實體對象查詢
這篇文章介紹了Entity?Framework模型優(yōu)先與實體對象查詢,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

