Solve the problem that Unity rotates and changes local coordinates

Abstract: When we make game character controllers, we often write the rotation of the character, but when we use Euler angles and quaternion rotations, we often rotate according to the local coordinates we don’t want, or want to It can be flexibly converted and used between the world coordinate system of the angle and its own coordinate system. For this reason, I wrote this article and hope it will be useful to everyone!

Application: The product law of quaternions

Product law: When the quaternion (Euler angled) is multiplied by the current rotation of the object, the object will be rotated according to the world coordinates;

When the quaternion is multiplied by * the current rotation of the object, the object will be rotated according to its own coordinates .

            float mouse_x = Input.GetAxis("Mouse X");
            float mouse_y = Input.GetAxis("Mouse Y");
            Quaternion qx = Quaternion.Euler(0, mouse_x, 0);
            Quaternion qy = Quaternion.Euler(-mouse_y, 0, 0);

            //记住当前乘积顺序
            transform.rotation = qx*transform.rotation ;//绕世界y坐标旋转

            transform.rotation = transform.rotation*qy;//绕自身坐标x旋转
            
            //对比

            transform.rotation = transform.rotation*qx ;//绕自身坐标y坐标旋转

            transform.rotation = qy*transform.rotation;//绕世界坐标x旋转

Guess you like

Origin blog.csdn.net/m0_64810555/article/details/125648290