Projext/Assets/Scripts/Player/Controller/PlayerInput.cs
2026-02-06 13:20:12 +09:00

68 lines
3.0 KiB
C#

using UnityEngine;
public class PlayerInput : MonoBehaviour
{
// 다른 스크립트에게 명령을 내려야 하니, 미리 참조 시킴
[SerializeField] private PlayerHealth health; // 죽었는지 확인용
[SerializeField] private PlayerMovement movement; // 이동 명령용
[SerializeField] private PlayerAim aim; // 회전 명령용
[SerializeField] private PlayerInteraction interaction; // 아이템 줍기 명령용
[SerializeField] private PlayerAttack attack;
[SerializeField] private PlayerStatsUI statsUI; // UI 창 열기 명령용
private void Update()
{
// 1. 사망 체크
// 죽었는데 키보드 눌린다고 시체가 움직이면 안 되니까 아예 입력을 차단(return)함
if (health != null && health.IsDead) return;
// 2. UI 토글 (C키)
// 스탯 창을 껐다 켰다 함
if (Input.GetKeyDown(KeyCode.C) && statsUI != null) statsUI.ToggleWindow();
// 3. 이동 입력 감지
// GetAxisRaw를 쓴 이유: 0에서 1로 부드럽게 변하는 게 아니라,
// 키를 누르면 즉시 1, 떼면 0이 되어서 빠릿빠릿한 조작감을 줌
float h = Input.GetAxisRaw("Horizontal"); // A, D 키 (-1, 0, 1)
float v = Input.GetAxisRaw("Vertical"); // W, S 키 (-1, 0, 1)
bool sprint = Input.GetKey(KeyCode.LeftShift); // Shift 누르고 있나?
// 4. 이동 명령 하달
if (movement != null)
{
// 방향 벡터를 정규화(.normalized)해서 대각선 이동 시 빨라지는 걸 방지
// 그리고 달리기 여부(sprint)도 같이 넘겨줍니다.
movement.SetMoveInput(new Vector3(h, 0, v).normalized, sprint);
// ⭐ [추가] 스페이스바 누르면 대시 명령!
if (Input.GetKeyDown(KeyCode.Space)) movement.AttemptDash();
}
// 5. 마우스 회전 명령
// 캐릭터가 마우스 커서를 바라보게 합니다.
if (aim != null) aim.RotateTowardsMouse();
// 6. 상호작용 (F키)
// 바닥에 떨어진 무기를 줍기
if (Input.GetKeyDown(KeyCode.F) && interaction != null) interaction.TryInteract();
// 7. 공격 입력 (좌클릭/우클릭)
if (attack != null)
{
// 우클릭 꾹: 차징(모으기) 시작
if (Input.GetMouseButtonDown(1)) attack.StartCharging();
// 우클릭 뗌: 차징 취소 (공격 안 하고 캔슬)
if (Input.GetMouseButtonUp(1)) attack.CancelCharging();
// 좌클릭: 공격 시도
if (Input.GetMouseButtonDown(0))
{
// 만약 우클릭(방어/조준) 상태에서 좌클릭을 했다면? -> 투척(Release)
if (Input.GetMouseButton(1)) attack.ReleaseAttack();
// 그냥 좌클릭만 했다면? -> 일반 평타(Normal Attack)
else attack.PerformNormalAttack();
}
}
}
}