Files
ProjectMD/Assets/Scripts/Player/PlayerMovement.cs
2026-01-13 01:20:44 +09:00

42 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
private Vector2 _moveInput;
private CharacterController _controller;
private PlayerInputActions _inputActions;
void Awake()
{
_controller = GetComponent<CharacterController>();
_inputActions = new PlayerInputActions();
// Move 액션 연결 (Vector2 타입으로 설정되어 있어야 함)
_inputActions.Player.Move.performed += ctx => _moveInput = ctx.ReadValue<Vector2>();
_inputActions.Player.Move.canceled += ctx => _moveInput = Vector2.zero;
}
void OnEnable() => _inputActions.Enable();
void OnDisable() => _inputActions.Disable();
void Update()
{
// 입력받은 방향으로 이동 계산
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y);
_controller.Move(move * Time.deltaTime * moveSpeed);
// 이동 중일 때 바라보는 방향 전환 (선택 사항)
if (move != Vector3.zero)
{
transform.forward = move;
}
}
// Player 스크립트 내부
void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log($"무언가와 부딪힘: {hit.gameObject.name}");
}
}