Unity first-person camera perspective control, mouse control lens rotation localEulerAngles

This script is mounted on the camera, and it can also be used directly by copying and pasting.

//定义欧拉角.
Vector3 angle = Vector3.zero;    //初始化属性xyz都为0.

//改变欧拉角.
angle.x = m_Transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * 100f * Time.deltaTime;
angle.y = m_Transform.localEulerAngles.y + Input.GetAxis("Mouse X") * 100f * Time.deltaTime;

//通过欧拉角 改变自身的欧拉旋转角度.
m_Transform.localEulerAngles = angle;

Interpret the meaning of the code:

The first three pieces of code are to change the value of the Euler angle defined by angle. Because the mouse only has the position of the screen, only Y and X need to be changed. As for the problem that the +- and XY axes are opposite, I haven’t found an explanation. If there is a big brother If you know, please let me know in the comment area, thank you.

The last piece of code is to change its own Euler rotation by dynamically changing the Euler angle value.

Explain the meaning of the code separately:

angle.x = m_Transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * 100f * Time.deltaTime;

Among them, m_Transform.localEulerAngles.x means: the value of the x-axis of the Euler rotation of the camera.

Input.GetAxis("Mouse Y") means: the position of the current mouse on the Y axis of the screen position.

Input.GetAxis("Mouse X") means: the position of the current mouse on the X-axis of the screen position.

100f * Time.deltaTime can be understood as mouse speed/mouse sensitivity.

If you need to know the second person, third person camera rotation, or more detailed first person, you can check this article:

https://blog.csdn.net/ghl1390490928/article/details/80139439

For a more detailed explanation of the localEulerAngles method:

https://blog.csdn.net/weixin_44739495/article/details/110680128?ops_request_misc=&request_id=&biz_id=102&utm_term=unity%20localEulerAngles%20&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-110680128.pc_search_result_control_group&spm=1018.2226.3001.4187

Other rotation methods:

https://blog.csdn.net/zxy13826134783/article/details/79461816?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163772680316780265457647%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163772680316780265457647&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-79461816.pc_search_result_control_group&utm_term=unity+localEulerAngles+&spm=1018.2226.3001.4187

おすすめ

転載: blog.csdn.net/weixin_55532142/article/details/121512552