unity中計算vector3的角度,帶正負的程式碼實現
阿新 • • 發佈:2019-01-11
unity中Vector3自帶了一個Vector3.Angle的函式,用於計算兩個vector3向量的夾角,文件中描述的:
The angle returned is always the non reflex angle between the two vectors – ie the smaller of the two possible angles between them and never greater than 180
參考:https://www.yxkfw.com/thread-57909-1-1.html
degrees.
這個角度返回的值是不大於180的度數,會造成逆時針120與240,返回的值其實是一樣的。
Snippet
/// <summary> /// Determine the signed angle between two vectors, with normal 'n' /// as the rotation axis. /// </summary> public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n) { return Mathf.Atan2( Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2)) * Mathf.Rad2Deg; }