Unity controla el movimiento de la cámara, el zoom y establece el rango de movimiento

public class FollowMouseMove : MonoBehaviour
{
    
    private float FollowPosx,FollowPosy;
    private float moveAmount=5f;   //鼠标控制镜头的移动速度,镜头的移动速度应该跟摄像头的尺寸相关
    private float ScrollWheelSpeed = 5f; //滚轮控制镜头的缩放速度
    private float Posx_max = 10;   //控制镜头x的最大尺寸,设置摄像头可查看范围
    private float Posy_max = 10;    //控制镜头y的最大尺寸 
    private float ScrollWheel_max = 50;   //滚轮控制镜头的最大缩放
    private float ScrollWheel_min = 5;    //滚轮控制镜头的最小缩放

    // Update is called once per frame
    void Update()
    {
        float Camer_Size = Camera.main.orthographicSize;
        moveAmount = Camer_Size / 2;   //控制镜头的移动速度和摄像头尺寸的关系,使其越大移动的越快
        if (Input.mousePosition.y >= Screen.height * 0.9 | Input.GetKey(KeyCode.W) )//如果鼠标位置在顶部,就向上移动
        {
            FollowPosy += moveAmount * Time.deltaTime;
        }
        if (Input.mousePosition.y <= Screen.height * 0.1 | Input.GetKey(KeyCode.S) )//如果鼠标位置在底部,就向下移动
        {
            FollowPosy -= moveAmount * Time.deltaTime;
        }

        if (Input.mousePosition.x >= Screen.width * 0.9 | Input.GetKey(KeyCode.D) )//如果鼠标位置在右侧,就向右移动
        {
            FollowPosx += moveAmount * Time.deltaTime;
        }
        if(Input.mousePosition.x <= Screen.width * 0.1 | Input.GetKey(KeyCode.A) ) //如果鼠标位置在左侧,就向左移动
        {
            FollowPosx -= moveAmount * Time.deltaTime;
        }
     
        //设置摄像头的可查看范围
        FollowPosx = Mathf.Clamp(FollowPosx, -Posx_max, Posx_max);
        FollowPosy = Mathf.Clamp(FollowPosy, -Posy_max, Posy_max);
        //调用坐标变换
        Camera.main.transform.position = new Vector3(FollowPosx, FollowPosy, 0);


        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //限制size缩放大小
            Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, ScrollWheel_min, ScrollWheel_max);
            //滚轮改变缩放
            Camera.main.orthographicSize = Camera.main.orthographicSize - Input.GetAxis("Mouse ScrollWheel") * ScrollWheelSpeed;
        }  

      
    }



}

El código anterior configura la cámara para mover la cámara de acuerdo con el mouse o WASD.

Supongo que te gusta

Origin blog.csdn.net/m0_71650342/article/details/130292500
Recomendado
Clasificación