Unity camera rotation with damping effect

insert image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class SmoothRotate_1 : MonoBehaviour
{
    
    
    [Header("旋转的类型")]
    public RoatationType _RoatationType;
    [Header("围绕旋转的目标物体")]
    public Transform target;

    [Header("设置旋转角度")]
    public float x = 0f, y = 0f, z = 0f;

    [Header("旋转速度值")]
    public float xSpeed = 10f, ySpeed = 10f, mSpeed = 5f;

    [Header(" y轴角度限制,设置成一样则该轴不旋转")]
    public float yMinLimit = -50, yMaxLimit = 80;


    [Header(" x轴角度限制")]
    public float leftMax = -365, rightMax = 365;

    [Header("距离限制")]
    public float distance = 6f, minDistance = 0.5f, maxDistance = 10f;

    [Header("阻尼设置")]
    public bool needDamping = true;
    [Header("阻尼大小")]
    public float damping = 3f;
    public float posdamping = 10;
    [Header("初始角度")]
    public float initX;

    public float initY;
    // 改变中心目标物体
    public void SetTarget(GameObject go)
    {
    
    
        target = go.transform;
    }
    void Start()
    {
    
    
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        //pers();
    }
    void LateUpdate()
    {
    
    
        if (target)
        {
    
    
            if (Input.GetMouseButton(0))
            {
    
    
                // 判断是否需要反向旋转
                if ((y > 90f && y < 270f) || (y < -90 && y > -270f))
                {
    
    
                    x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }
                else

                {
    
    
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

              //  x = ClampAngle(x, leftMax, rightMax);

                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }

            distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;

            distance = Mathf.Clamp(distance, minDistance, maxDistance);

           

            //Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
            Debug.Log(distance);

            //旋转角度
            Quaternion rotation = Quaternion.Euler(y, x, z);

            //位置
            Vector3 position = rotation * disVector + target.position;
           // Vector3 position = rotation * disVector + target.position;

            // 阻尼感
            if (needDamping)
            {
    
    
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                if(_RoatationType==RoatationType.user)
                {
    
    
                    transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * posdamping);
                }
               
            }
            else
            {
    
    
                transform.rotation = rotation;
                if (_RoatationType == RoatationType.user)
                {
    
    
                    transform.position = position;
                }
              
            }
        }
    }
    // 对数值进行限制;
    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);
    }
    // 初始
    public void pers()
    {
    
    
        this.x = initX;
        this.y = initY;
    }
    // 正视图
    public void front()
    {
    
    
        this.x = 0f;
        this.y = 0f;
    }
    // 后视图
    public void back()
    {
    
    
        this.x = 180f;
        this.y = 0f;
    }
    // 左视图
    public void left()
    {
    
    
        this.x = 90f;
        this.y = 0f;
    }
    // 右视图
    public void right()
    {
    
    
        this.x = 270f;
        this.y = 0f;
    }
    // 俯视图
    public void top()
    {
    
    
        this.x = 0f;
        this.y = 90f;
    }
    // 仰视图
    public void bottom()
    {
    
    
        this.x = 0f;
        this.y = -90f;
    }
}
public enum RoatationType
{
    
    
    self,
    user,
}

Guess you like

Origin blog.csdn.net/qiao2037641855/article/details/124063933