Unity3D 四元数旋转使用 Quaternion

           //增量a是180度的百分比

           //debug
            if (Input.GetKeyDown(KeyCode.D))
            {
                Debug.Log(gameObject.transform.right.ToString() + "     x");
                Debug.Log(gameObject.transform.up.ToString() + "     y");
                Debug.Log(gameObject.transform.forward.ToString() + "     z");
            }
            
            //打印当前Quaternion的值
            if (Input.GetKeyDown(KeyCode.Q))
            {
                Debug.Log(gameObject.transform.rotation.ToString() + "     q");
            }
            
            //调试增量
            if (Input.GetKeyDown(KeyCode.O))
            {
                a += 0.1f;
                Debug.Log(a.ToString());
            }
            
            //调试增量
            if (Input.GetKeyDown(KeyCode.P))
            {
                a -= 0.1f;
                Debug.Log(a.ToString());
            }

            //输出调试增量
            if (Input.GetKeyDown(KeyCode.S))
            {
                Debug.Log(a.ToString());
            }

            //清空调试增量
            if (Input.GetKeyDown(KeyCode.B))
            {
                a = 0.0f;
                Debug.Log(a.ToString());
            }

            /*绕单一轴旋转
            if (Input.GetKeyDown(KeyCode.X))
            {
                gameObject.transform.rotation = new Quaternion(1,0,0, (Mathf.PI - Mathf.PI * a));
            }
            */
            
            
            //绕三个轴旋转一个调用周期只能执行一个操作
            if (Input.GetKeyDown(KeyCode.X))
            {
                gameObject.transform.rotation = new Quaternion(gameObject.transform.up.x, gameObject.transform.up.y, gameObject.transform.up.z, (Mathf.PI - Mathf.PI * a));
            }
            
            //绕三个轴旋转一个调用周期只能执行一个操作
            if (Input.GetKeyDown(KeyCode.Y))
            {
                gameObject.transform.rotation = new Quaternion(gameObject.transform.right.x, gameObject.transform.right.y,gameObject.transform.right.z, (Mathf.PI - Mathf.PI * a));
            }
            
            //绕三个轴旋转一个调用周期只能执行一个操作
            if (Input.GetKeyDown(KeyCode.Z))
            {
                gameObject.transform.rotation = new Quaternion(gameObject.transform.forward.x, gameObject.transform.forward.y, gameObject.transform.forward.z, (Mathf.PI - Mathf.PI * a));
            }

            //移动一个轴到指定位置
            if (Input.GetKeyDown(KeyCode.T))
            {
                Quaternion q;
                q.x = q.y = q.z = 0.0f;
                q.w = 1.0f;

                q.SetFromToRotation(gameObject.transform.forward, gameObject.transform.up);
                gameObject.transform.rotation = q;
                Debug.Log("ttttt");
                //SetFromToRotation(gameObject.transform.forward, gameObject.transform.up);
            }
               

你可能感兴趣的:(Unity3D,四元数,Quaternion)