78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using System;
|
|
using RPGCore.Core;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace RPGCore.Movement.SceneModules.ActionController
|
|
{
|
|
[Serializable]
|
|
public class ActionCameraModule : SceneModule
|
|
{
|
|
[Header("Camera Rotate and Tilt")]
|
|
[SerializeField, Range(0.01f, 1f)] private float _rotateSensitivity = 0.1f;
|
|
[SerializeField, Range(0.01f, 1f)] private float _tiltSensitivity = 0.06f;
|
|
[SerializeField] private Vector2 _tiltLimit = new(20f, 85f); // 10 - 90
|
|
|
|
[Header("Camera Zoom")]
|
|
[SerializeField, Range(0.05f, 1f)] private float _zoomSensitivity = 0.3f;
|
|
[SerializeField] private Vector2 _zoomLimit = new(2f, 15f); // 1 - 30
|
|
[SerializeField] private float _zoomCurrent = 8f;
|
|
|
|
[Header("Input Actions")]
|
|
[SerializeField] private InputActionReference _rotateAndTiltInput;
|
|
[SerializeField] private InputActionReference _zoomInput;
|
|
|
|
// RUNTIME
|
|
private Camera _camera;
|
|
|
|
[Header("Camera target position")]
|
|
public Transform stickTo;
|
|
[SerializeField] private Vector3 _offset;
|
|
|
|
private void Awake()
|
|
{
|
|
_camera = Camera.main;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
RotateAndTiltCamera();
|
|
ZoomCamera();
|
|
MoveCamera();
|
|
}
|
|
|
|
private void RotateAndTiltCamera()
|
|
{
|
|
if (!_rotateAndTiltInput || !_rotateAndTiltInput.action.inProgress) return;
|
|
|
|
var delta = _rotateAndTiltInput.action.ReadValue<Vector2>();
|
|
var tiltDelta = delta.y * _tiltSensitivity * -1;
|
|
var rotateDelta = delta.x * _rotateSensitivity;
|
|
|
|
var previousEuler = _camera.transform.rotation.eulerAngles;
|
|
var nextEuler = previousEuler + new Vector3(tiltDelta, rotateDelta, 0);
|
|
|
|
if (nextEuler.x < _tiltLimit.x) nextEuler.x = _tiltLimit.x;
|
|
if (nextEuler.x > _tiltLimit.y) nextEuler.x = _tiltLimit.y;
|
|
|
|
_camera.transform.rotation = Quaternion.Euler(nextEuler);
|
|
}
|
|
|
|
private void ZoomCamera()
|
|
{
|
|
if (!_zoomInput || !_zoomInput.action.inProgress) return;
|
|
|
|
_zoomCurrent -= _zoomInput.action.ReadValue<float>() * _zoomSensitivity;
|
|
|
|
if (_zoomCurrent > _zoomLimit.y) _zoomCurrent = _zoomLimit.y;
|
|
if (_zoomCurrent < _zoomLimit.x) _zoomCurrent = _zoomLimit.x;
|
|
}
|
|
|
|
private void MoveCamera()
|
|
{
|
|
var toPosition = stickTo.position - _camera.transform.forward * _zoomCurrent;
|
|
_camera.transform.position = toPosition + _camera.transform.TransformDirection(_offset);
|
|
}
|
|
}
|
|
} |