这篇文章主要为大家详细介绍了Unity实现绕任意轴任意角度旋转向量,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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实现绕任意轴任意角度旋转向量
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- C# windows语音识别与朗读实例 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# 调用WebService的方法 2023-03-09
- C#类和结构详解 2023-05-30