콜라이더 충돌 시 npc 대사 출력 기능 구현 및 HP 바 기능 구현

This commit is contained in:
김민구
2026-07-08 03:50:56 +09:00
parent b160214799
commit 8f742e50d9
5 changed files with 412 additions and 373 deletions
@@ -0,0 +1,54 @@
using UnityEngine;
[RequireComponent(typeof(Collider2D))] // 감지를 위한 Collider2D(Trigger 설정)가 필수적으로 요구됩니다.
public class NPCInteraction : MonoBehaviour
{
private NPCDialogueManager dialogueManager;
private void Start()
{
// 1. 씬에 배치된 NPCDialogueManager 컴포넌트를 탐색해 바인딩합니다.
dialogueManager = FindObjectOfType<NPCDialogueManager>();
if (dialogueManager == null)
{
Debug.LogError("[NPC] 씬에서 NPCDialogueManager를 찾을 수 없습니다! DialogueManager 오브젝트가 배치되어 있는지 확인하세요.");
}
// 2. 부착된 콜라이더가 트리거(Trigger)로 체크되어 있지 않다면 강제로 설정하여 물리 막힘 현상을 배제합니다.
Collider2D col = GetComponent<Collider2D>();
if (col != null && !col.isTrigger)
{
col.isTrigger = true;
Debug.LogWarning($"[NPC] {gameObject.name}에 부착된 Collider2D의 'Is Trigger' 속성을 자동으로 켰습니다.");
}
}
// 플레이어가 NPC의 감지 반경(Trigger Collider) 내부로 들어왔을 때 실행
private void OnTriggerEnter2D(Collider2D other)
{
// 충돌한 오브젝트가 플레이어인지 검사합니다.
Player player = other.GetComponent<Player>();
if (player != null)
{
if (dialogueManager != null)
{
Debug.Log($"[NPC] 플레이어({player.CharacterName})가 가까이 왔습니다. 세대({player.Generation}대) 조언을 요청합니다.");
dialogueManager.RequestNPCDialogue(player.Generation);
}
}
}
// 플레이어가 NPC의 감지 반경을 벗어났을 때 실행
private void OnTriggerExit2D(Collider2D other)
{
Player player = other.GetComponent<Player>();
if (player != null)
{
if (dialogueManager != null)
{
Debug.Log("[NPC] 플레이어가 멀어졌습니다. 대화창을 닫습니다.");
dialogueManager.CloseDialogue();
}
}
}
}