Мне нужен совет, у меня есть класс CameraController using Cinemachine; using…
- User
Мне нужен совет, у меня есть класс CameraController
using Cinemachine; using UnityEditor; using UnityEngine; using UnityEngine.Serialization;
public class CameraController : MonoBehaviour { [SerializeField] private InputHandler inputHandler; [SerializeField] private InputProvider inputProvider; [SerializeField] private CinemachineVirtualCamera virtualCamera; private CinemachineFramingTransposer _framingTransposer;
public bool isFreezing; [SerializeField] [Tooltip("Check to make the controller be independent on the Time Scale.")] public bool independentTimeScale = true; public bool IsDragging { get; private set; } public bool IsMoving { get; private set; } public bool IsRotating { get; private set; } public bool IsEdgeScrolling { get; private set; } [SerializeField] [Space(10)] [Header("Move")] private float moveSpeed; [field: SerializeField] public bool AllowMove { get; set; } [FormerlySerializedAs("_dragStartPosition")] [SerializeField] [Space(10)] [Header("Drag")] [Tooltip("Режет дистанцию если драг слишком большой")] private float maxDragMagnitude; private Vector3 _dragStartPosition; [Tooltip("На сколько сильно увеличивать движение после того как игрок отпустил пкм. 1 - без изменений")] [SerializeField] private float dragPanScale; [Tooltip("На сколько сильно уменьшать дистанцию после того как игрок отпустил пкм. 1 - без изменений")] [SerializeField, Range(1, 10)] private float dragPanDeltaReduce; [SerializeField] private float cameraDragPanSmoothTime; [SerializeField] private float cameraDragSmoothTime; [field: SerializeField] public bool AllowDrag { get; set; } private Vector3 _lastDragDelta; private Vector3 _targetCameraPosition; private Vector3 _currentCameraPosition; [SerializeField] [Space(10)] [Header("Zoom")] private float cameraZoomSpeed; private float _currentCameraZoom; private float _cameraZoomSmoothDampVelRef; [SerializeField] private float cameraZoomMin; [SerializeField] private float cameraZoomMax; [SerializeField] private float cameraZoomSmoothTime = 7f; [field: SerializeField] public bool AllowZoom { get; set; } [SerializeField] [Space(10)] [Header("Rotate")] private bool mouseLockOnRotate = true; [SerializeField] private float rotateSpeed; [SerializeField] private float cameraTiltMin; [SerializeField] private float cameraTiltMax; [SerializeField] private bool invertMouseHorizontal; [SerializeField] private bool invertMouseVertical; [SerializeField] private float cameraTargetRotateSmoothTime; private float _targetCameraRotate; private float _targetCameraTilt; private float _currentCameraRotate; private float _currentCameraTilt; private float _cameraRotateSmoothDampVelRef; private float _cameraTiltSmoothDampVelRef; [field: SerializeField] public bool AllowRotate { get; set; } [FormerlySerializedAs("_edgeScrollingSpeed")] [Space(10)] [Header("Edge Scrolling")] [SerializeField] private float edgeScrollingSpeed; [SerializeField] private float edgeScrollingAreaOffset; [field: SerializeField] public bool AllowEdgeScrolling { get; set; } [SerializeField] [Space(10)] [Header("Boundaries")] [Range(-10000, 0)] public float boundaryMinX = -500f; [SerializeField, Range(0, 10000)] public float boundaryMaxX = 500f; [SerializeField, Range(-10000, 0)] public float boundaryMinZ = -500f; [SerializeField, Range(0, 10000)] public float boundaryMaxZ = 500f; [field: SerializeField] public bool EnableBoundaries { get; set; } = true; private void Start() { if (!inputHandler) inputHandler = GetComponent<InputHandler>(); if (!inputProvider) inputProvider = GetComponent<InputProvider>(); _framingTransposer = virtualCamera.GetCinemachineComponent<CinemachineFramingTransposer>(); _currentCameraZoom = _framingTransposer.m_CameraDistance; _currentCameraTilt = Mathf.Lerp(cameraTiltMin, cameraTiltMax, 0.5f); _targetCameraTilt = _currentCameraTilt; _targetCameraRotate = transform.eulerAngles.y; inputHandler.OnDragStop += () => { IsDragging = false; // _currentCameraPosition = transform.position; // _targetCameraPosition = transform.position; var moveInput = _lastDragDelta / dragPanDeltaReduce * dragPanScale; _targetCameraPosition += moveInput; }; inputHandler.OnDrag += dragPosition => { if (isFreezing) return; if (!AllowDrag) return; if (IsRotating) return; if (!IsDragging) { //Да, OnDragStart для пидоров IsDragging = true; _dragStartPosition = dragPosition; _currentCameraPosition = transform.position; _targetCameraPosition = transform.position; } var dragDelta = _dragStartPosition - dragPosition; if (dragDelta.magnitude > maxDragMagnitude) { dragDelta = dragDelta.normalized * maxDragMagnitude; } // transform.position += dragDelta; _targetCameraPosition += dragDelta; _lastDragDelta = dragDelta; }; inputHandler.OnMove += inputVector => { if (isFreezing) return; if (!AllowMove) return; var moveDirection = GetRelativeDirection(inputVector); var moveInput = moveDirection * (moveSpeed * RelativeSpeed * Time.deltaTime); transform.position += moveInput; }; inputHandler.OnMoveStart += () => { IsMoving = true; }; inputHandler.OnMoveStop += () => { IsMoving = false; _currentCameraPosition = transform.position; _targetCameraPosition = transform.position; }; inputHandler.OnRotate += inputVector => { if (isFreezing) return; if (!AllowRotate) return; if (IsDragging) return; IsRotating = true; if (mouseLockOnRotate) LockMouse(true); if (inputVector.x != 0) { _targetCameraRotate += inputVector.x * rotateSpeed * (invertMouseHorizontal ? -1 : 1); } if (inputVector.y != 0) { _targetCameraTilt -= inputVector.y * rotateSpeed * (invertMouseVertical ? -1 : 1); _targetCameraTilt = Mathf.Clamp(_targetCameraTilt, cameraTiltMin, cameraTiltMax); } }; inputHandler.OnRotateStop += () => { IsRotating = false; if (mouseLockOnRotate) LockMouse(false); }; inputHandler.OnZoom += value => { if (isFreezing) return; if (!AllowZoom) return; _currentCameraZoom -= value * cameraZoomSpeed; _currentCameraZoom = Mathf.Clamp(_currentCameraZoom, cameraZoomMin, cameraZoomMax); }; } private void LockMouse(bool lockMouse) { Cursor.lockState = lockMouse ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !lockMouse; } private void Update() { if (isFreezing) return; ProcessRotate(); ProcessDrag(); ProcessDragPan(); ProcessZoom(); HandleEdgeScrolling(); } private void LateUpdate() { if (!EnableBoundaries) return; HandleBoundaries(); } private void ProcessDrag() { if (!IsDragging) return; _currentCameraPosition = Vector3.Lerp(_currentCameraPosition, _targetCameraPosition, cameraDragSmoothTime / 100); transform.position = _currentCameraPosition; _currentCameraPosition = transform.position; _targetCameraPosition = transform.position; } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.