using System.Collections; using System.Collections.Generic; using Cinemachine; using UnityEngine; using UnityEngine.InputSystem; public class CameraManager : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float maxSpeed = 5f; [SerializeField] private float acceleration = 10f; [SerializeField] private float damping = 15f; [SerializeField] private CinemachineVirtualCamera camera; RoomBuilder roomBuilder; InputAction movement; Transform cameraTransform; private Vector3 lastPosition; private Vector3 horizontalVelocity; private Vector3 targetPosition; private void Awake() { roomBuilder = new RoomBuilder(); cameraTransform = camera.transform; } private void OnEnable() { cameraTransform.LookAt(this.transform); lastPosition = this.transform.position; movement = roomBuilder.Camera.Movement; roomBuilder.Camera.Enable(); } private void OnDisable() { roomBuilder.Camera.Disable(); } private Vector3 GetCameraForward() { Vector3 forward = cameraTransform.forward; forward.y = 0f; return forward; } private Vector3 GetCameraRight() { Vector3 right = cameraTransform.right; right.y = 0f; return right; } private void UpdateVelocity() { horizontalVelocity = (this.transform.position - lastPosition) / Time.deltaTime; horizontalVelocity.y = 0; lastPosition = this.transform.position; } private void GetKeyboardMovement() { Vector3 inputValue = movement.ReadValue().x * GetCameraRight() + movement.ReadValue().y * GetCameraForward(); inputValue = inputValue.normalized; if (inputValue.sqrMagnitude > 0.1f) { targetPosition = inputValue; } } private void UpdateBasePosition() { if (targetPosition.sqrMagnitude > 0.1f) { speed = Mathf.Lerp(speed, maxSpeed, Time.deltaTime * acceleration); transform.position += targetPosition * speed * Time.deltaTime; } else { horizontalVelocity = Vector3.Lerp(horizontalVelocity, targetPosition, Time.deltaTime * damping); transform.position += horizontalVelocity * Time.deltaTime; } targetPosition = Vector3.zero; } private void Update() { GetKeyboardMovement(); UpdateVelocity(); UpdateBasePosition(); } }