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

本文通过网上查阅资料,通过自己的理解。把四元数最直接的用法记录下来

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);
            }
}


猜你喜欢

转载自blog.csdn.net/q510264505/article/details/70332001