using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour { [SerializeField] private float moveSpeed = 5f; private Vector2 _moveInput; private CharacterController _controller; private PlayerInputActions _inputActions; private Vector3 _velocity; public float gravity = -19.62f; // 조금 더 묵직하게 설정 void Awake() { _controller = GetComponent(); _inputActions = new PlayerInputActions(); // Move 액션 연결 (Vector2 타입으로 설정되어 있어야 함) _inputActions.Player.Move.performed += ctx => _moveInput = ctx.ReadValue(); _inputActions.Player.Move.canceled += ctx => _moveInput = Vector2.zero; } void OnEnable() => _inputActions.Enable(); void OnDisable() => _inputActions.Disable(); void Update() { // 1. 수직 속도(중력) 계산 if (_controller.isGrounded && _velocity.y < 0) { // 바닥에 닿아있을 때 속도를 아주 작게 유지 (경사로 등에서 안정적) _velocity.y = -2f; } // 2. 입력받은 방향으로 평면 이동 계산 Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y); // 수평 이동 실행 _controller.Move(move * Time.deltaTime * moveSpeed); // 3. 중력 가속도 적용 (v = v + g * t) _velocity.y += gravity * Time.deltaTime; // 4. 수직 이동(중력) 실행 (s = v * t) _controller.Move(_velocity * Time.deltaTime); // 이동 중일 때 바라보는 방향 전환 if (move != Vector3.zero) { transform.forward = move; } } }