欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年01月19日 10:32:01   作者:類人_猿  
這篇文章主要為大家詳細(xì)介紹了Unity向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下

一、unity的旋轉(zhuǎn)

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

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

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

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

案例:將Vector3(1,0,1)按照原點(diǎn)旋轉(zhuǎn)45°,90°,180°,270°測(cè)試分別用黑、黃、藍(lá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°測(cè)試分別用紅、黃、藍(lá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;
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論