using UnityEngine; using UnityEngine.InputSystem; namespace KirbyAdventures { [RequireComponent(typeof(CharacterController))] public class PlayerMotor : MonoBehaviour { public float MoveSpeed = 6f; public float JumpHeight = 1.4f; public float Gravity = -22f; public float GroundStick = -2f; public Transform CameraTransform; public Transform YawPivot; CharacterController _cc; Vector3 _velocity; bool _grounded; void Awake() { _cc = GetComponent(); } void Update() { if (GameFlow.Instance != null && (GameFlow.Instance.IsPaused || GameFlow.Instance.HasWon)) return; HandleRespawnInput(); ReadMoveAndJump(); } void HandleRespawnInput() { var kb = Keyboard.current; if (kb != null && kb.rKey.wasPressedThisFrame) CheckpointSystem.Respawn(transform, _cc); } void ReadMoveAndJump() { var kb = Keyboard.current; float h = 0f, v = 0f; if (kb != null) { if (kb.aKey.isPressed || kb.leftArrowKey.isPressed) h -= 1f; if (kb.dKey.isPressed || kb.rightArrowKey.isPressed) h += 1f; if (kb.sKey.isPressed || kb.downArrowKey.isPressed) v -= 1f; if (kb.wKey.isPressed || kb.upArrowKey.isPressed) v += 1f; } _grounded = _cc.isGrounded; if (_grounded && _velocity.y < 0f) _velocity.y = GroundStick; Vector3 forward = Vector3.forward; Vector3 right = Vector3.right; if (YawPivot != null) { forward = YawPivot.forward; right = YawPivot.right; } else if (CameraTransform != null) { forward = CameraTransform.forward; right = CameraTransform.right; } forward.y = 0f; right.y = 0f; forward.Normalize(); right.Normalize(); Vector3 move = (forward * v + right * h); if (move.sqrMagnitude > 1f) move.Normalize(); _cc.Move(move * MoveSpeed * Time.deltaTime); if (kb != null && kb.spaceKey.wasPressedThisFrame && _grounded) _velocity.y = Mathf.Sqrt(JumpHeight * -2f * Gravity); _velocity.y += Gravity * Time.deltaTime; _cc.Move(_velocity * Time.deltaTime); if (move.sqrMagnitude > 0.01f) { var look = Quaternion.LookRotation(move, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, look, 12f * Time.deltaTime); } } } }