using UnityEngine; using Unheimlich.Managers; namespace Unheimlich.Gameplay.Player { /// /// Handles player movement, jumping, and gravity using CharacterController. /// Provides movement state information for other systems like camera head bob. /// [RequireComponent(typeof(CharacterController))] public class PlayerMovement : MonoBehaviour { public static PlayerMovement Instance { get; private set; } [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; private void Awake() { if (Instance != null && Instance != this) { Destroy(this); return; } Instance = this; CharacterController = GetComponent(); } 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; if (PlayerInputManager.Instance.JumpAction.action.WasPressedThisFrame()) _ySpeed = _jumpForce; } else _ySpeed += Physics.gravity.y * Time.deltaTime; _moveDirection.y = _ySpeed; CharacterController.Move(_moveDirection * Time.deltaTime); } } }