Unity: The mouse [when sliding up, down, left, and right] controls the camera [looks left and right] and [looks up and down]

The rotation of the camera seems to be a trivial matter, but it is related to the user's intuitive experience. If the rotation is right, the mother is kind and the son is filial, but if the rotation is wrong, the world will be overwhelmed.

1. Function

When the mouse moves left and right, control the camera to turn left and right
When the mouse moves up and down, control the camera to look up and down

2. The process of being overturned by GPT

You can ask questions in GPT, and his answers are also very powerful and can be transferred, but none of them conform to the characteristics of the human body to see things.

Later, I wrote it myself...
There are two key points in the camera rotation in this case:

  • 1. When the mouse slides left and right, the camera should rotate around the Y axis of the world, and the center of rotation is at the origin of the camera.
//左右旋转:绕自己的原点旋转,旋转轴为世界的Y轴
this.transform.RotateAround(this.transform.position, axisX,mouseX);//  public Vector3 axisY = new Vector3(1,0,0);

Analogy:
Although I am lowering my head (the camera is looking down), when I look around, the east and west are around the Y axis where I stand, which is the Y axis of the world, to rotate. Any other rotation method will lead to strange effects, which do not conform to the characteristics of human observation.

The coordinate axes in the figure below are the world coordinate axes.

The coordinate axis in the figure is the world coordinate axis

- 2、鼠标上下滑动的时候,相机实现抬头和低头的动作,此时相机是绕自己的x轴进行旋转。
类比:把自己的头想象成一个摄像机,抬头和低头时,我们绕的轴是穿过太阳穴的一条线。
//抬头低头:绕自己的轴旋转
transform.Rotate(axisY,mouseY, Space.Self);// public Vector3 axisY = new Vector3(1,0,0);

Please add a picture description

3. Code

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

/// <summary>
/// 鼠标左右移动时,控制相机左右转动
/// 鼠标上下移动时,控制相机抬头低头
/// 该脚本挂载到相机Camera上
/// Todo:加入平滑插值或者动画效果
/// </summary>
public class CameraController2 : MonoBehaviour
{
    
    
    /// <summary>
    /// 鼠标灵敏度
    /// </summary>
    [Header("鼠标灵敏度")]
    public float sensitivity = 100f; // 鼠标灵敏度

    /// <summary>
    /// 相机抬头低头的旋转轴
    /// </summary>
    [Header("相机抬头低头的旋转轴")]
    public Vector3 axisY = new Vector3(1,0,0);
    
    /// <summary>
    /// 鼠标X位移映射的旋转轴
    /// </summary>
    [Header("鼠标X位移映射的旋转轴")]
    public Vector3 axisX = new Vector3(0, 1, 0);
    
    [Header("0-左键,1-右键,2-中建")]
    [SerializeField]
    public int buttonIndex = 1;   
    
    void Update()
    {
    
    
        //鼠标按下
        if (Input.GetMouseButton(buttonIndex))
        {
    
    
            // 获取鼠标的位置变化delta值
            float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
            float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;

            //左右旋转:绕自己的点旋转,旋转轴为世界的Y轴
            this.transform.RotateAround(this.transform.position, axisX,mouseX);

            //抬头低头:绕自己的轴旋转
            transform.Rotate(axisY,mouseY, Space.Self);
        }
    }
}

Guess you like

Origin blog.csdn.net/dzj2021/article/details/131112186