64 lines
1.7 KiB
C#
64 lines
1.7 KiB
C#
using UnityEngine;
|
|
using Unheimlich.Core;
|
|
using Unheimlich.Managers;
|
|
|
|
namespace Unheimlich.Gameplay.Player
|
|
{
|
|
[RequireComponent(typeof(CharacterController))]
|
|
public class PlayerMovement : Singleton<PlayerMovement>
|
|
{
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private float _moveSpeed = 5f;
|
|
[SerializeField] private float _jumpForce = 10f;
|
|
[SerializeField] private float _groundedGravity = -2f;
|
|
|
|
public CharacterController CharacterController { get; private set; }
|
|
|
|
public bool IsMoving { get; private set; }
|
|
|
|
private float _ySpeed;
|
|
private Vector3 _moveDirection;
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
CharacterController = GetComponent<CharacterController>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!PlayerInputManager.IsInputEnabled || CharacterController == null)
|
|
return;
|
|
|
|
HandleMovement();
|
|
}
|
|
|
|
private void HandleMovement()
|
|
{
|
|
Vector2 input = PlayerInputManager.Instance.MoveInput;
|
|
|
|
Vector3 forward = transform.forward * input.y;
|
|
Vector3 right = transform.right * input.x;
|
|
|
|
_moveDirection = forward + right;
|
|
_moveDirection = _moveDirection.normalized * _moveSpeed;
|
|
|
|
IsMoving = _moveDirection.magnitude > 0.1f && CharacterController.isGrounded;
|
|
|
|
if (CharacterController.isGrounded)
|
|
{
|
|
_ySpeed = _groundedGravity;
|
|
|
|
var jumpAction = PlayerInputManager.GetJumpAction();
|
|
if (jumpAction != null && jumpAction.WasPressedThisFrame())
|
|
_ySpeed = _jumpForce;
|
|
}
|
|
else
|
|
_ySpeed += Physics.gravity.y * Time.deltaTime;
|
|
|
|
_moveDirection.y = _ySpeed;
|
|
CharacterController.Move(_moveDirection * Time.deltaTime);
|
|
}
|
|
}
|
|
}
|