Unity鼠标键控制摄像头的旋转,滚轮控制摄像机的推进

 控制相机的拖动旋转

        float dx = Input.GetAxis("Mouse X");
        float dy = Input.GetAxis("Mouse Y");
        if (Input.GetMouseButton(0))
        {
            dx *= rotationSpeed;
            dy *= rotationSpeed;           
            transform.Rotate(Vector3.right* dy* rotationSpeed * Time.fixedDeltaTime,Space.Self);//自身旋转
            transform.Rotate(Vector3.up * dx* rotationSpeed * Time.fixedDeltaTime, Space.World);//世界旋转
        }
        //获取四元素
        angles = transform.localEulerAngles;//0~360
        if (angles.x < 180)
        {
            if (angles.x > 45)
            {
                transform.rotation = Quaternion.Euler(new Vector3(45, angles.y, angles.z));
            }
        }
        if (angles.x > 180)
        {
            if (angles.x < 360 - 45)
            {
                transform.rotation = Quaternion.Euler(new Vector3(360 - 45, angles.y, angles.z));
            }
        }

 滚轮控制相机的位移

 //鼠标滚轮控制摄像头的推进
        if (Input.GetAxis("Mouse ScrollWheel")!=0)
        {
            transform.Translate(Vector3.forward*Time.deltaTime * Input.GetAxis("Mouse ScrollWheel") * 25, Space.Self);
        }

猜你喜欢

转载自blog.csdn.net/m0_71624363/article/details/130871049