欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司主營江口網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),江口h5小程序設(shè)計搭建,江口網(wǎng)站營銷推廣歡迎江口等地區(qū)企業(yè)咨詢

一、unity的旋轉(zhuǎn)

首先要知道一點(diǎn)就是在Unity的旋轉(zhuǎn)中使用過四元數(shù)進(jìn)行旋轉(zhuǎn)的,如果對一個物體的rotation直接賦值你會發(fā)現(xiàn)結(jié)果不是你最終想要的結(jié)果,這個時候我們需要去借助Quaternion來進(jìn)行旋轉(zhuǎn)。

二、向量按照原點(diǎn)進(jìn)行旋轉(zhuǎn)

用到的Unity內(nèi)置方法Quaternion.AngleAxis(float angle,Vector3 axis)
第一個參數(shù)就是我們需要旋轉(zhuǎn)的角度 angle大于0時是按照順時針的方向進(jìn)行旋轉(zhuǎn),angle小于0是按照逆時針的方向旋轉(zhuǎn),這里的旋轉(zhuǎn)時按照坐標(biāo)原點(diǎn)進(jìn)行的旋轉(zhuǎn)。
第二個參數(shù)是旋轉(zhuǎn)軸,圍繞哪一個坐標(biāo)軸進(jìn)行旋轉(zhuǎn)。

注意:使用這個方法時獲得的也是四元數(shù),我們將其轉(zhuǎn)換成向量Vector3是需要乘以自身的坐標(biāo)(四元數(shù) * 自身向量,如果反過來 自身向量 * 四元數(shù) 在Unity會發(fā)生編譯錯誤,這里需要注意一下)

案例:將Vector3(1,0,1)按照原點(diǎn)旋轉(zhuǎn)45°,90°,180°,270°測試分別用黑、黃、藍(lán)、綠顏色表示

Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)

代碼如下:

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);

 }
}

三、向量按照指定位置進(jìn)行旋轉(zhuǎn)

/// <summary>
/// 圍繞某點(diǎn)旋轉(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°測試分別用紅、黃、藍(lán)、綠顏色表示

Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)

代碼如下:

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>
 /// 圍繞某點(diǎn)旋轉(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;
 }
}

關(guān)于Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁名稱:Unity中怎么將向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article10/cohodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站微信公眾號網(wǎng)頁設(shè)計公司面包屑導(dǎo)航品牌網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司