| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | using DG.Tweening;using System;using Unity.Mathematics;using Unity.VisualScripting;using UnityEngine;using UnityEngine.EventSystems;public class CameraBird : MonoBehaviour{    public Transform target; // 目标对象,摄像头将朝向此对象    public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度    public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度    public float rotateSpeed = 0.25f;    public float translateSpeed = 3f;    public float minDistance = 5f; // 摄像头与目标对象的最小距离    public float maxDistance = 50f; // 摄像头与目标对象的最大距离    public float currentDistance; // 当前摄像头与目标对象的距离    private Vector3 offset; // 摄像头与目标对象的偏移量    private Vector3 velocity = Vector3.zero; // 摄像头移动的速度    private bool isDragging = false; // 是否正在拖拽    private Vector3 lastMousePosition; // 上一帧鼠标位置    public float rotateYAngle = 0.0f;    public float rotateXAngle = 0.0f;    bool isRotate = false;    bool onUI = false;    public Action OnCameraBeginChange;    public Action OnCameraEndChange;    private bool isChanging = false;    public bool onScroll;    float scrollCountDown = 0.5f;    void Start()    {        // 初始化当前距离为初始偏移量的距离        currentDistance = Vector3.Distance(transform.position, target.position);        // 确保初始距离在允许的范围内        currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);        // 计算偏移量        offset = transform.position - target.position;    }    private bool IsPointerOverUIElement()    {        // 检查当前鼠标位置是否在 UI 上        return EventSystem.current.IsPointerOverGameObject();    }    void LateUpdate()    {        onUI = false;        if (IsPointerOverUIElement()) onUI = true;        this.GetComponent<Camera>().nearClipPlane = currentDistance / 100.0f;        // 鼠标滚轮控制摄像头远近        if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)        {            // 更新当前距离            currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;            // 确保距离在允许的范围内            currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);            scrollCountDown = 0.5f;            onScroll = true;            //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;        }        else {            scrollCountDown -= Time.deltaTime;            if (scrollCountDown < 0) {                onScroll = false;            }        }        translateSpeed = currentDistance / 600f;        scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;        scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);        if (Input.GetMouseButton(2))        {            rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;            rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;            rotateXAngle = Math.Clamp(rotateXAngle, 2, 1000);            isRotate = true;        }        else {            isRotate = false;        }        // 鼠标左键拖拽平移摄像头        if (Input.GetMouseButtonDown(1) && !onUI)        {            isDragging = true;            lastMousePosition = Input.mousePosition;        }        else if (Input.GetMouseButtonUp(1))        {            isDragging = false;        }        if (isDragging)        {            // 计算鼠标移动的偏移量            Vector3 mouseOffset = Input.mousePosition - lastMousePosition;            // 根据偏移量计算摄像头的目标位置            Vector3 moveDirection = new Vector3(transform.forward.x, 0, transform.forward.z) * -mouseOffset.y * translateSpeed + new Vector3(transform.right.x, 0, transform.right.z) * -mouseOffset.x * translateSpeed;            // 平滑移动摄像头到目标位置            target.position += moveDirection;            // 计算目标位置            Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;            // 摄像头平滑移动到目标位置            transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);        }        else        {            if (isRotate)            {                Blink();            }            else            {                // 计算目标位置                Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;                // 摄像头平滑移动到目标位置                transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);            }        }        lastMousePosition = Input.mousePosition;    }    public void Blink()    {        // 计算目标位置        Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;        // 摄像头平滑移动到目标位置        transform.position = targetPosition;        //摄像头朝向目标对象        transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);    }    public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)    {        if (onUI) {            return;        }        MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();        fader.gameObject.SetActive(true);        fader.material.DOColor(Color.white, 0.1f).onComplete = () =>        {            fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>            {                fader.gameObject.SetActive(false);            };        };        this.target.localPosition = centerPos;        DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);        this.Blink();    }    void Update()    {        if (isRotate || isDragging || onScroll) {            if (!isChanging) {                isChanging = true;                OnCameraBeginChange?.Invoke();            }        }        else{            if (isChanging) {                isChanging = false;                OnCameraEndChange?.Invoke();            }        }    }}
 |