这篇文章给大家分享的是有关Unity如何实现绕任意轴任意角度旋转向量的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体内容如下
游戏中有一需求,就是一个矩形或者Cube绕着某一点旋转任意角度,现在给出下面算法。
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; }
测试用例
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); } }
效果图
感谢各位的阅读!关于“Unity如何实现绕任意轴任意角度旋转向量”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。