123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class ModelCameraCtrl : MonoBehaviour
- {
- public static ModelCameraCtrl _Instance;
- public Camera _camera;
- public float smoothSpeed = 5f; // 摄像头平滑移动的速度
- public float scrollSensitivity = 65f; // 鼠标滚轮的灵敏度
- public float rotateSpeed =3f;
- public float translateSpeed = 0.8f;
- public float minDistance = 5f; // 摄像头与目标对象的最小距离
- public float maxDistance = 50f; // 摄像头与目标对象的最大距离
- public float currentDistance; // 当前摄像头与目标对象的距离
- private Vector3 offset; // 摄像头与目标对象的偏移量
- private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
- public float rotateYAngle = 0.0f;
- public float rotateXAngle = 0.0f;
- bool isRotate = false;
- bool onUI = false;
- private void Awake()
- {
- _Instance = this;
- }
- public void SetCameraActive(bool flag)
- {
- _camera.enabled = flag;
- }
- private void Start()
- {
- SetCameraActive(false);
- // 初始化当前距离为初始偏移量的距离
- currentDistance = Vector3.Distance(_camera.transform.position, this.transform.position);
- // 确保初始距离在允许的范围内
- currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
- offset = _camera.transform.position - this.transform.position;
- }
- public void SetCameraPos(Transform pos,float distance,Vector2 rota)
- {
- currentDistance = distance;
- this.transform.position = pos.position;
- rotateXAngle = rota.x;
- rotateYAngle = rota.y;
- }
- void LateUpdate()
- {
- onUI = false;
- if (IsPointerOverUIElement()) onUI = true;
- // 鼠标滚轮控制摄像头远近
- if (Input.GetAxis("Mouse ScrollWheel") != 0 && onUI)
- {
- // 更新当前距离
- currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
- // 确保距离在允许的范围内
- currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
- //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
- }
- translateSpeed = currentDistance / 600f;
- scrollSensitivity = currentDistance / 16.0f * currentDistance / 16.0f;
- scrollSensitivity = Mathf.Clamp(scrollSensitivity, 10, 4000);
- if (Input.GetMouseButton(0)&& onUI)
- {
- rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
- rotateYAngle += Input.GetAxis("Mouse X") * rotateSpeed;
- isRotate = true;
- }
- else
- {
- isRotate = false;
- }
- if (isRotate)
- {
- Blink();
- }
- else
- {
- // 计算目标位置
- Vector3 targetPosition = this.transform.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
- offset.normalized * currentDistance;
- // 摄像头平滑移动到目标位置
- _camera.transform.position = Vector3.Lerp(_camera.transform.position, targetPosition, Time.deltaTime * smoothSpeed);
- }
- }
- private bool IsPointerOverUIElement()
- {
- // 检查当前鼠标位置是否在 UI 上
- return EventSystem.current.IsPointerOverGameObject();
- }
- public void Blink()
- {
- // 计算目标位置
- Vector3 targetPosition = this.transform.position +
- Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized *
- currentDistance;
- // 摄像头平滑移动到目标位置
- _camera.transform.position = targetPosition;
- //摄像头朝向目标对象
- _camera.transform.transform.rotation =
- Quaternion.LookRotation(this.transform.position - _camera.transform.position, Vector3.up);
- }
- }
|