64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class HeroCameraController : MonoBehaviour
|
|
{
|
|
|
|
[Range(1f, 10f)]
|
|
[SerializeField] private float _mouseSensitivityX = 2f;
|
|
[Range(1f, 10f)]
|
|
[SerializeField] private float _mouseSensitivityY = 2f;
|
|
[Range(0f, 0.5f)]
|
|
[SerializeField] private float _smoothTime = 0.1f;
|
|
[SerializeField] private float _minVerticalAngle = -75f;
|
|
[SerializeField] private float _maxVerticalAngle = 45f;
|
|
[SerializeField] private Transform _controller;
|
|
|
|
private float _xRotation = 0f;
|
|
private float _yRotation = 0f;
|
|
private Vector2 _currentMouseDelta;
|
|
private Vector2 _currentMouseVelocity;
|
|
|
|
private void Start()
|
|
{
|
|
Cursor.lockState = CursorLockMode.Confined;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
Vector2 targetMouseDelta = new Vector2(
|
|
Input.GetAxis("Mouse X") * _mouseSensitivityX,
|
|
Input.GetAxis("Mouse Y") * _mouseSensitivityY
|
|
);
|
|
|
|
_currentMouseDelta = Vector2.SmoothDamp(
|
|
_currentMouseDelta,
|
|
targetMouseDelta,
|
|
ref _currentMouseVelocity,
|
|
_smoothTime
|
|
);
|
|
|
|
_yRotation += _currentMouseDelta.x;
|
|
_xRotation -= _currentMouseDelta.y;
|
|
_xRotation = Mathf.Clamp(_xRotation, _minVerticalAngle, _maxVerticalAngle);
|
|
|
|
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
|
|
|
|
if (_controller != null)
|
|
_controller.rotation = Quaternion.Euler(0f, _yRotation, 0f);
|
|
}
|
|
|
|
public void SetCameraActive(bool active) => enabled = active;
|
|
public void SetSensitivityX(float value) => _mouseSensitivityX = Mathf.Clamp(value, 1f, 10f);
|
|
public void SetSensitivityY(float value) => _mouseSensitivityY = Mathf.Clamp(value, 1f, 10f);
|
|
public void SetSmoothTime(float value) => _smoothTime = Mathf.Clamp(value, 0f, 0.5f);
|
|
public void SetSensitivity(float value)
|
|
{
|
|
_mouseSensitivityX = Mathf.Clamp(value, 1f, 10f);
|
|
_mouseSensitivityY = Mathf.Clamp(value, 1f, 10f);
|
|
}
|
|
}
|
|
|