Update
This commit is contained in:
66
Assets/Scripts/Camera Controller/CameraRotationHandler.cs
Normal file
66
Assets/Scripts/Camera Controller/CameraRotationHandler.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
|
||||
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)
|
||||
{
|
||||
if (inputReader == null) return;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user