unity鼠标右键旋转视角

   
using UnityEngine;

public class CameraSwitch : MonoBehaviour
{
    private float lastPosX;
    private float lastPosY;
    public Transform CameraX;
    public Transform CameraY;
    private float RotaSpeed ;
    public float Speed = 100;
    private float range = 2;
    
    void Update()
    {
        RotaSpeed = Speed * Time.deltaTime;

        if (Input.GetMouseButton(1))
        {
            if (Input.mousePosition.x > lastPosX + range)
            {
                CameraX.Rotate(Vector3.up, RotaSpeed);
                lastPosX = Input.mousePosition.x;

            }
            else if (Input.mousePosition.x < lastPosX - range)
            {
                CameraX.Rotate(Vector3.up, -RotaSpeed);
                lastPosX = Input.mousePosition.x;
            }

            if (Input.mousePosition.y > lastPosY + range)
            {
                CameraY.Rotate(Vector3.right, -RotaSpeed);
                lastPosY = Input.mousePosition.y;

            }
            else if (Input.mousePosition.y < lastPosY - range)
            {
                CameraY.Rotate(Vector3.right, RotaSpeed);
                lastPosY = Input.mousePosition.y;
            }
        }
    }
}

你可能感兴趣的:(unity鼠标右键旋转视角)