Initial commit
This commit is contained in:
88
Assets/Scripts/HeroCameraController.cs
Normal file
88
Assets/Scripts/HeroCameraController.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HeroCameraController : MonoBehaviour
|
||||
{
|
||||
[Header("Íàëàøòóâàííÿ ìèø³")]
|
||||
[Range(1f, 10f)]
|
||||
[SerializeField] private float _mouseSensitivityX = 2f;
|
||||
|
||||
[Range(1f, 10f)]
|
||||
[Tooltip("×óòëèâ³ñòü ìèø³ ïî âåðòèêàë³")]
|
||||
[SerializeField] private float _mouseSensitivityY = 2f;
|
||||
|
||||
[Range(0f, 0.5f)]
|
||||
[SerializeField] private float _smoothTime = 0.1f;
|
||||
|
||||
[Header("Îáìåæåííÿ êóò³â")]
|
||||
[SerializeField] private float _minVerticalAngle = -75f;
|
||||
|
||||
[SerializeField] private float _maxVerticalAngle = 45f;
|
||||
|
||||
[Header("Ïîñèëàííÿ")]
|
||||
[SerializeField] private Transform _controller;
|
||||
|
||||
|
||||
private float _xRotation = 0f;
|
||||
private float _yRotation = 0f;
|
||||
|
||||
private Vector2 _currentMouseDelta;
|
||||
private Vector2 _currentMouseVelocity;
|
||||
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
HandleCameraRotation();
|
||||
}
|
||||
|
||||
private void HandleCameraRotation()
|
||||
{
|
||||
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 SetSensitivityX(float sensitivity)
|
||||
{
|
||||
_mouseSensitivityX = Mathf.Clamp(sensitivity, 1f, 10f);
|
||||
}
|
||||
|
||||
public void SetSensitivityY(float sensitivity)
|
||||
{
|
||||
_mouseSensitivityY = Mathf.Clamp(sensitivity, 1f, 10f);
|
||||
}
|
||||
|
||||
public void SetSmoothTime(float smoothTime)
|
||||
{
|
||||
_smoothTime = Mathf.Clamp(smoothTime, 0f, 0.5f);
|
||||
}
|
||||
|
||||
public void SetSensitivity(float sensitivity)
|
||||
{
|
||||
_mouseSensitivityX = Mathf.Clamp(sensitivity, 1f, 10f);
|
||||
_mouseSensitivityY = Mathf.Clamp(sensitivity, 1f, 10f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user