您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Unity如何實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
游戲中有一需求,就是一個(gè)矩形或者Cube繞著某一點(diǎn)旋轉(zhuǎn)任意角度,現(xiàn)在給出下面算法。
public static Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) { Vector3 point = Quaternion.AngleAxis(angle, axis) * (position - center); Vector3 resultVec3 = center + point; return resultVec3; }
測(cè)試用例
using UnityEngine; using System.Collections; public class RotateTest : MonoBehaviour { public LineRenderer line1; public LineRenderer line2; public float angle = 30f; private Vector3 v0; private Vector3 v1; private Vector3 v2; private Vector3 v3; private Vector3 v4; private Vector3 vCenter; void Start() { v0 = new Vector3(3f,0f,1f); v1 = new Vector3(1f, 0f, 3f); v2 = new Vector3(4f, 0f, 6f); v3 = new Vector3(6f, 0f, 4f); vCenter = new Vector3(2f, 0f, 2f); } // Use this for initialization void Update () { line1.SetVertexCount(5); line1.SetPosition(0,v0); line1.SetPosition(1,v1); line1.SetPosition(2,v2); line1.SetPosition(3,v3); line1.SetPosition(4,v0); line2.SetVertexCount(5); Vector3 v01 = MathUtils.RotateRound(v0, vCenter, Vector3.up, angle); Vector3 v11 = MathUtils.RotateRound(v1, vCenter, Vector3.up, angle); Vector3 v21 = MathUtils.RotateRound(v2, vCenter, Vector3.up, angle); Vector3 v31 = MathUtils.RotateRound(v3, vCenter, Vector3.up, angle); Vector3 v41 = MathUtils.RotateRound(v4, vCenter, Vector3.up, angle); line2.SetPosition(0, v01); line2.SetPosition(1, v11); line2.SetPosition(2, v21); line2.SetPosition(3, v31); line2.SetPosition(4, v01); } }
效果圖
感謝各位的閱讀!關(guān)于“Unity如何實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。