Unity, Euler angle

introduce

In Unity, Euler Angles are used to describe the rotation state of an object, which consists of three angles, usually pitch angle (Pitch), yaw angle (Yaw) and roll angle (Roll). In the Transform component, the Euler angle of the object can be obtained or set through the eulerAngles property. For example, when an object rotates around the X axis, its Euler angle is expressed as (X, 0, 0), when the object rotates around the Y axis, its Euler angle is expressed as (0, Y, 0), when the object When rotating around the Z axis, its Euler angles are expressed as (0, 0, Z). Euler angles are very commonly used in game development and can be used to control the rotation state of objects and achieve various dynamic effects. However, it should be noted that there is a universal lock problem in Euler angles, and attention should be paid to avoid abnormal situations.

Gimbal Lock: Gimbal Lock is an abnormal condition in the Euler angle rotation. When the pitch angle of the object is close to 90 degrees or -90 degrees, it will cause abnormal rotation. Specifically, when the pitch angle of the object is close to 90 degrees, the angles of the object's rotation around the Y axis and the rotation around the Z axis will overlap, causing the object to not be able to rotate freely, but a "stuck" phenomenon of rotation.


method

In Unity, the Euler angle of the object can be obtained or set through the eulerAngles property of the Transform component. For example, the Euler angles of an object can be obtained using the following code:

Vector3 euler = transform.eulerAngles;
You can also use the following code to set the Euler angle of the object:

transform.eulerAngles = new Vector3(x, y, z);
Among them, x, y, and z represent the rotation angles of the object around the X-axis, Y-axis, and Z-axis, respectively.

In addition to using the eulerAngles property, you can also use the Quaternion.Euler() method to convert Euler angles to quaternions. For example, the following code can be used to convert Euler angles to quaternions:

Quaternion rotation = Quaternion.Euler(x, y, z);
Among them, x, y, and z represent the rotation angles of the object around the X-axis, Y-axis, and Z-axis, respectively.


for example

mouse control rotation view

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{ // mouse sensitivity public float mouseSensitivity = 100f; // angle limit public float clampAngle = 80f;



// 鼠标移动距离
private float mouseX;
private float mouseY;

// 角度
private float rotX = 0f;
private float rotY = 0f;

void Start()
{
    // 获取初始角度
    Vector3 rot = transform.localRotation.eulerAngles;
    rotX = rot.x;
    rotY = rot.y;
}

void Update()
{
    // 获取鼠标移动距离
    mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    // 旋转视角
    rotX -= mouseY;
    rotY += mouseX;

    // 角度限制
    rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

    // 更新旋转
    Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0f);
    transform.rotation = localRotation;
}

}


Guess you like

Origin blog.csdn.net/qq_20179331/article/details/130012573