init
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9460d10ec97244dbb73bb37ab1aec54e
|
||||
timeCreated: 1766851215
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using RPGCore.Movement.ObjectModules.UnitMovement;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
using RPGCore.Movement.ObjectModules.UnitMovement.Actions;
|
||||
using RPGCore.ObjectModules.ActionObjectModule;
|
||||
using RPGCoreCommon.Helpers;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace RPGCore.Movement.SceneModules.ActionController
|
||||
{
|
||||
[Serializable]
|
||||
[RequireComponent(typeof(ActionCameraModule))]
|
||||
public class ActionControllerModule : SceneModule
|
||||
{
|
||||
[SerializeField] private UnitObject _unit;
|
||||
[SerializeField] private InputActionReference _rotateInput;
|
||||
[SerializeField] private InputActionReference _moveInput;
|
||||
[SerializeField] private InputActionReference _moveFasterInput;
|
||||
[SerializeField] private InputActionReference _jumpInput;
|
||||
|
||||
private Camera _camera;
|
||||
private BaseActionParallel _moveAction;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_camera = Camera.main;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var movementModule = _unit.GetComponent<UnitMovementModule>();
|
||||
|
||||
_rotateInput.action.performed += _ =>
|
||||
{
|
||||
movementModule.rotateYaw = _camera.transform.rotation.eulerAngles.y;
|
||||
};
|
||||
|
||||
_moveInput.action.performed += _ =>
|
||||
{
|
||||
_moveAction = new DirectionMoveAction(GetDirection);
|
||||
_unit.actions.Execute(_moveAction);
|
||||
};
|
||||
_moveInput.action.canceled += _ =>
|
||||
{
|
||||
_moveAction?.CancelIt();
|
||||
_moveAction = null;
|
||||
};
|
||||
|
||||
_moveFasterInput.action.performed += _ => movementModule.isSprinting = true;
|
||||
_moveFasterInput.action.canceled += _ => movementModule.isSprinting = false;
|
||||
|
||||
_jumpInput.action.performed += _ => _unit.actions.Execute(new JumpAction());
|
||||
|
||||
movementModule.rotateType = UnitMovementRotateType.CustomDirection;
|
||||
}
|
||||
|
||||
private Vector3 GetDirection()
|
||||
{
|
||||
var input = _moveInput.action.ReadValue<Vector2>();
|
||||
return _camera.transform.right.SetY(0).normalized * input.x
|
||||
+ _camera.transform.forward.SetY(0).normalized * input.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f54b0d5fd319467da310ffc54509377a
|
||||
timeCreated: 1766851215
|
||||
Reference in New Issue
Block a user