您的位置:首页 > 移动开发 > Unity3D

unity3d 四元数最直观的用法(旋转)

2017-04-21 16:50 218 查看
本文通过网上查阅资料,通过自己的理解。把四元数最直接的用法记录下来

gameObject.transform.rotation = Quaternion.Euler(30, 0, 0);//旋转x轴角度
gameObject.transform.rotation = Quaternion.EulerAngles(π/n, 0, 0);	//π/n(n是整数,得到角度 1=57.3度
gameObject.transform.rotation = Quaternion.AngleAxis(30, Vector3.right);//旋转x角度


Vector3.right(right可以改为left,up,down,fwd,forward)具体向什么方向旋转,自己去测试

下面是一个左右摆动的例子:

private float time0 = 0;
private float change;
void Update () {

time0 += Time.deltaTime; //左右摆动
change = Mathf.Repeat(time0 * 0.4f, 2);//从0数到2,循环,比下面的时间慢一倍
if (change >= 1)
{
gameObject.transform.rotation = Quaternion.EulerAngles(Mathf.PingPong(time0 * 0.8f, 1), 0, 0);
}
else
{
gameObject.transform.rotation = Quaternion.EulerAngles(-1 * Mathf.PingPong(time0 * 0.8f, 1), 0, 0);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 四元数 旋转