鼠标拖动照相机带惯性

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class g_1 : MonoBehaviour {
        //围绕的中心点
        public Transform target;
        //距离
        public float normalDistance = 10;
        //x的速度
        private float xSpeed = 150.0f;
        //y的速度
        private float ySpeed = 150.0f;
        //y的最小旋转角度
        private int yMinLimit = 30;
        //y的最大旋转角度
        private int yMaxLimit = 80;
        //绕x轴的角度
        private float x = 0.0f;
        //绕y轴的角度
        private float y = 0.0f;
        //
        private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
        //记录中心点的位置
        private Vector3 CameraTarget;
        //记录x轴手指滑动的方向
        float xm;
        //记录y轴手指滑动的方向
        float ym;



        void Start()
        {

            CameraTarget = target.position;

            float z = target.transform.position.z - normalDistance;
            transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);

            transform.LookAt(target);

            var angles = transform.eulerAngles;
            x = angles.y;
            y = angles.x;
        }

        void Update()
        {

            if (Input.GetMouseButton(1))
            {
            xSpeed = 150.0f;
            ySpeed = 150.0f;
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            xm = Input.GetAxis("Mouse X");
            ym = Input.GetAxis("Mouse Y");
            Debug.Log("x:" + x + "y:" + y);
            y = ClampAngle(y, yMinLimit, yMaxLimit);

            var rotation = Quaternion.Euler(y, x, 0);
            var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;

            transform.rotation = rotation;
            transform.position = position;
            transform.LookAt(CameraTarget);


            }else {
            if (xSpeed >= 0 || ySpeed >= 0)
            {
                xSpeed = Mathf.Lerp(xSpeed, 0, 2.0f * Time.deltaTime);
                ySpeed = Mathf.Lerp(ySpeed, 0, 2.0f * Time.deltaTime);
                if (xm > 0)
                {
                    x += xSpeed * 0.02f;

                    if (ym > 0)
                    {
                        y -= ySpeed * 0.02f;
                    }else if (ym < 0){
                        y -= -ySpeed * 0.02f;
                    }
                }

                if (xm < 0)
                {
                    x += -xSpeed * 0.02f;

                    if (ym > 0)
                    {
                        y -= ySpeed * 0.02f;
                    }
                    else if (ym < 0)
                    {
                        y -= -ySpeed * 0.02f;
                    }
                }

                Debug.Log("x:" + x + "y:" + y);
                y = ClampAngle(y, yMinLimit, yMaxLimit);

                var rotation = Quaternion.Euler(y, x, 0);
                var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;

                transform.rotation = rotation;
                transform.position = position;
                transform.LookAt(CameraTarget);

            }

        }
            
      
    }

        static float ClampAngle(float angle, float min, float max)
        {
            if (angle < -360)
                angle += 360;
            if (angle > 360)
                angle -= 360;
            return Mathf.Clamp(angle, min, max);
        }

      
}

推荐阅读更多精彩内容