Files
BABA_YAGA/Assets/Scripts/Camera Controller/CameraRotationHandler.cs
2026-03-27 12:27:50 +07:00

67 lines
2.6 KiB
C#

using UnityEngine;
using static OnlyScove.Scripts.CameraController;
namespace OnlyScove.Scripts
{
[System.Serializable]
public class CameraRotationHandler
{
[Header("Rotation Settings")]
[SerializeField] private float sensitivity = 0.1f;
[SerializeField] private float minVerticalAngle = -45f;
[SerializeField] private float maxVerticalAngle = 45f;
[SerializeField] private bool invertX;
[SerializeField] private bool invertY;
[Header("Auto Rotation")]
[SerializeField] private bool useAutoRotation = true;
[SerializeField] private float autoRotateDelay = 2.5f;
[SerializeField] private float autoRotateSpeed = 2f;
private float _rotationX;
private float _rotationY;
private float _lastInputTime;
public Quaternion CurrentRotation { get; private set; }
public Quaternion PlanarRotation => Quaternion.Euler(0f, _rotationY, 0f);
public void Initialize(Transform cameraTransform)
{
_rotationX = cameraTransform.eulerAngles.x;
_rotationY = cameraTransform.eulerAngles.y;
_lastInputTime = Time.time;
CurrentRotation = cameraTransform.rotation;
}
public void HandleRotation(InputReader inputReader, Transform followTarget, float rotationSmoothTime, CameraViewMode viewMode)
{
if (inputReader == null || viewMode == CameraViewMode.FirstPerson) return; // Only process input in TPV
if (inputReader.LookInput.magnitude > 0.01f)
{
_lastInputTime = Time.time;
}
float invertXVal = (invertX) ? -1 : 1;
float invertYVal = (invertY) ? -1 : 1;
_rotationX -= inputReader.LookInput.y * invertYVal * sensitivity * Time.deltaTime;
_rotationX = Mathf.Clamp(_rotationX, minVerticalAngle, maxVerticalAngle);
_rotationY += inputReader.LookInput.x * invertXVal * sensitivity * Time.deltaTime;
// Auto-Correction
if (useAutoRotation && Time.time - _lastInputTime > autoRotateDelay)
{
if (inputReader.MoveInput.magnitude > 0.1f)
{
float targetYaw = followTarget.eulerAngles.y;
_rotationY = Mathf.LerpAngle(_rotationY, targetYaw, autoRotateSpeed * Time.deltaTime);
}
}
Quaternion targetRotation = Quaternion.Euler(_rotationX, _rotationY, 0f);
CurrentRotation = Quaternion.Slerp(CurrentRotation, targetRotation, rotationSmoothTime * Time.deltaTime);
}
}
}