美文网首页
unity鼠标右键旋转视角

unity鼠标右键旋转视角

作者: Zi_Fi | 来源:发表于2021-02-17 21:46 被阅读0次
       
    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鼠标右键旋转视角

          本文链接:https://www.haomeiwen.com/subject/tknbxltx.html