unity-----------------------四元數與歐拉旋轉方法


轉:http://blog.csdn.net/treepulse/article/details/49281295

Transfrom.eulerAngles

歐拉角

public float yRotation = 5.0F; void Update() { yRotation += Input.GetAxis("Horizontal"); transform.eulerAngles = new Vector3(10, yRotation, 0); //x軸和z軸的旋轉角度始終不變,只改變繞y軸的旋轉角度 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Transfrom.rotation

旋轉角度

//重設世界的旋轉角度 transform.rotation = Quaternion.identity;
  • 1
  • 2
//平滑傾斜物體向一個target旋轉 public float smooth = 2.0F; public float tiltAngle = 30.0F; void Update() { float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle; float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle; Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ); //向target旋轉阻尼 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Transfrom.RotateAround

這里寫圖片描述

//圍繞世界坐標原點,每秒20度物體自旋 transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);
  • 1
  • 2

Transfrom.Rotate

這里寫圖片描述
這里寫圖片描述

這里寫圖片描述

//沿着x軸每秒1度慢慢的旋轉物體 transform.Rotate(Vector3.right * Time.deltaTime); //相對於世界坐標沿着y軸每秒1度慢慢的旋轉物體 transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
  • 1
  • 2
  • 3
  • 4

Transfrom.LookAt

這里寫圖片描述 
這里寫圖片描述 
當該物體設置了LookAt並指定了目標物體時,該物體的z軸將始終指向目標物體,在設置了worldUp軸向時,該物體在更接近指定的軸向時旋轉便的靈活,注意worldUp指的是世界空間,不論你物體在什么位置,只要接近指定的軸方向,旋轉會變的更靈活。

//這個完成的腳本附加到一個攝像機,使它連續指向另一個物體。 //target變量作為一個屬性顯示在檢視面板上 //拖動另一個物體到這個上面,是攝像機注視它 public Transform target; //每幀旋轉攝像機,這樣它保持注視目標 void Update() { transform.LookAt(target); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
// Point the object at the world origin //使物體注視世界坐標原點 transform.LookAt(Vector3.zero);
  • 1
  • 2
  • 3

Quaternion.LookRotation

這里寫圖片描述

//大多數時間可以使用transform.LookAt代替 public Transform target; void Update() { Vector3 relativePos = target.position - transform.position; Quaternion rotation = Quaternion.LookRotation(relativePos); transform.rotation = rotation; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Quaternion.Angle

這里寫圖片描述

//計算transform和target之間的旋轉角度 public Transform target; void Update() { float angle = Quaternion.Angle(transform.rotation, target.rotation); }
  • 1
  • 2
  • 3
  • 4
  • 5

Quaternion.Eulr

這里寫圖片描述 
這里寫圖片描述

//繞y軸旋轉30度 public Quaternion rotation = Quaternion.Euler(0, 30, 0);
  • 1
  • 2

Quaternion.Slerp

這里寫圖片描述

//在from和to之間插值旋轉. //(from和to不能與附加腳本的物體相同) public Transform from; public Transform to; public float speed = 0.1F; void Update() { transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其實任何角度都可以沒有限制

Quaternion.FromToRotation

這里寫圖片描述

//設置旋轉,變換的y軸轉向z軸 transform.rotation = Quaternion.FromToRotation (Vector3.up, transform.forward);
  • 1
  • 2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM