美文网首页
五、Unity摄像机围绕物体旋转两种实现方式

五、Unity摄像机围绕物体旋转两种实现方式

作者: 李昀熹1112 | 来源:发表于2021-07-22 11:04 被阅读0次
    方法一:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CamerMoveTest2 : MonoBehaviour {
    
        public Transform target;//获取旋转目标
            public float speed = 0.5f;
    
        private void Update()
        {
            if (Input.GetMouseButton(0))
            {
                camerarotate();
            }
           
            camerazoom();
        }
    
        private void camerarotate() //摄像机围绕目标旋转操作
        {
            transform.RotateAround(target.position, Vector3.up, speed*Time.deltaTime); //摄像机围绕目标旋转
            var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
            var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
    
            if (Input.GetKey(KeyCode.Mouse1))
            {
                transform.Translate(Vector3.left*(mouse_x*15f)*Time.deltaTime);
                transform.Translate(Vector3.up*(mouse_y*15f)*Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.Mouse0))
            {
                transform.RotateAround(target.transform.position, Vector3.up, mouse_x*5);
                transform.RotateAround(target.transform.position, transform.right, mouse_y*5);
            }
        }
    
        private void camerazoom() //摄像机滚轮缩放
        {
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
                transform.Translate(Vector3.forward*0.5f);
      
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
                transform.Translate(Vector3.forward*-0.5f);
        }
    }
    
    
    方法二:
    using System.Collections;  
    using System.Collections.Generic;  
    using UnityEngine;  
      
    public class CameraMove : MonoBehaviour  
    {  
        //摄像机距离  
        public float distance = 10.0f;  
        //缩放系数  
        public float scaleFactor = 1f;  
      
      
        public float maxDistance = 30f;  
        public float minDistance = 5f;  
      
      
        //记录上一次手机触摸位置判断用户是在左放大还是缩小手势  
        private Vector2 oldPosition1;  
        private Vector2 oldPosition2;  
      
      
        private Vector2 lastSingleTouchPosition;  
      
        private Vector3 m_CameraOffset;  
        private Camera m_Camera;  
      
        public bool useMouse = true;  
      
        //定义摄像机可以活动的范围  
        public float xMin = -100;  
        public float xMax = 100;  
        public float zMin = -100;  
        public float zMax = 100;  
      
        //这个变量用来记录单指双指的变换  
        private bool m_IsSingleFinger;  
      
        //初始化游戏信息设置  
        void Start()  
        {  
            m_Camera = this.GetComponent<Camera>();  
            m_CameraOffset = m_Camera.transform.position;  
        }  
      
        void Update()  
        {  
            //判断触摸数量为单点触摸  
            if (Input.touchCount == 1)  
            {  
                if (Input.GetTouch(0).phase == TouchPhase.Began || !m_IsSingleFinger)  
                {  
                    //在开始触摸或者从两字手指放开回来的时候记录一下触摸的位置  
                    lastSingleTouchPosition = Input.GetTouch(0).position;  
                }  
                if (Input.GetTouch(0).phase == TouchPhase.Moved)  
                {  
                    MoveCamera(Input.GetTouch(0).position);  
                }  
                m_IsSingleFinger = true;  
      
            }  
            else if (Input.touchCount > 1)  
            {  
                //当从单指触摸进入多指触摸的时候,记录一下触摸的位置  
                //保证计算缩放都是从两指手指触碰开始的  
                if (m_IsSingleFinger)  
                {  
                    oldPosition1 = Input.GetTouch(0).position;  
                    oldPosition2 = Input.GetTouch(1).position;  
                }  
      
                if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)  
                {  
                    ScaleCamera();  
                }  
      
                m_IsSingleFinger = false;  
            }  
      
      
            //用鼠标的  
            if (useMouse)  
            {  
                distance -= Input.GetAxis("Mouse ScrollWheel") * scaleFactor;  
                distance = Mathf.Clamp(distance, minDistance, maxDistance);  
                if (Input.GetMouseButtonDown(0))  
                {  
                    lastSingleTouchPosition = Input.mousePosition;  
                    Debug.Log("GetMouseButtonDown:" + lastSingleTouchPosition);  
                }  
                if (Input.GetMouseButton(0))  
                {  
                    MoveCamera(Input.mousePosition);  
                }  
            } 
        }  
      
        /// <summary>  
        /// 触摸缩放摄像头  
        /// </summary>  
        private void ScaleCamera()  
        {  
            //计算出当前两点触摸点的位置  
            var tempPosition1 = Input.GetTouch(0).position;  
            var tempPosition2 = Input.GetTouch(1).position;  
      
      
            float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);  
            float lastTouchDistance = Vector3.Distance(oldPosition1, oldPosition2);  
      
            //计算上次和这次双指触摸之间的距离差距  
            //然后去更改摄像机的距离  
            distance -= (currentTouchDistance - lastTouchDistance) * scaleFactor * Time.deltaTime;  
      
      
            //把距离限制住在min和max之间  
            distance = Mathf.Clamp(distance, minDistance, maxDistance);  
      
      
            //备份上一次触摸点的位置,用于对比  
            oldPosition1 = tempPosition1;  
            oldPosition2 = tempPosition2;  
        }  
      
      
        //Update方法一旦调用结束以后进入这里算出重置摄像机的位置  
        private void LateUpdate()  
        {  
            var position = m_CameraOffset + m_Camera.transform.forward * -distance;  
            m_Camera.transform.position = position;  
        }  
      
      
        private void MoveCamera(Vector3 scenePos)  
        {  
            Vector3 lastTouchPostion = m_Camera.ScreenToWorldPoint(new Vector3(lastSingleTouchPosition.x, lastSingleTouchPosition.y, -1));  
            Vector3 currentTouchPosition = m_Camera.ScreenToWorldPoint(new Vector3(scenePos.x, scenePos.y, -1));  
      
            Vector3 v = currentTouchPosition - lastTouchPostion;  
            m_CameraOffset += new Vector3(v.x, 0, v.z) * m_Camera.transform.position.y;  
      
            //把摄像机的位置控制在范围内  
            m_CameraOffset = new Vector3(Mathf.Clamp(m_CameraOffset.x, xMin, xMax), m_CameraOffset.y, Mathf.Clamp(m_CameraOffset.z, zMin, zMax));  
            //Debug.Log(lastTouchPostion + "|" + currentTouchPosition + "|" + v);  
            lastSingleTouchPosition = scenePos;  
        }  
    }  
    

    相关文章

      网友评论

          本文标题:五、Unity摄像机围绕物体旋转两种实现方式

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