콜라이더 충돌 시 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
+27 -1
View File
@@ -1,4 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public struct Inputs
{
@@ -29,7 +31,7 @@ public class Player : MonoBehaviour
// 이동 관련 변수
[SerializeField] private float speed = 5f;
[SerializeField] public int hp = 5;
private int maxHp;
[SerializeField] public int maxHp = 5;
private bool isFacingRight = false;
private int currentAnimationState = -1;
@@ -43,6 +45,10 @@ public class Player : MonoBehaviour
public int Generation => generation;
public string CharacterName => characterName;
[Header("UI 연결")]
public TextMeshProUGUI nameText;
public Image hpImage;
private static readonly string[] RANDOM_NAMES = new string[]
{
"자르반", "가렌", "럭스", "다리우스", "야스오", "티모", "아리", "이즈리얼",
@@ -80,6 +86,22 @@ public class Player : MonoBehaviour
private void Update()
{
// 안전 조치: maxHp가 비정상(0 이하)이거나 현재 체력이 초과된 경우 자동 동기화
if (maxHp <= 0 || hp > maxHp)
{
maxHp = hp;
}
if (hpImage != null && maxHp > 0)
{
float fillRatio = Mathf.Clamp01((float)hp / maxHp);
hpImage.fillAmount = fillRatio;
// 이미지 컴포넌트의 타입(Simple, Filled 등)과 상관없이 무조건 가로 크기가 깎이도록
// RectTransform의 로컬 스케일 X축도 비율에 맞춰 실시간으로 동기화합니다.
hpImage.rectTransform.localScale = new Vector3(fillRatio, 1f, 1f);
}
if (hp <= 0)
{
if (animator != null)
@@ -256,6 +278,10 @@ public class Player : MonoBehaviour
// 이름 랜덤 선택 후 세대 접미사 추가 (예: 자르반 3세)
string baseName = RANDOM_NAMES[Random.Range(0, RANDOM_NAMES.Length)];
characterName = $"{baseName} {generation}세";
if (nameText != null)
{
nameText.text = characterName;
}
Debug.Log($"캐릭터 생성 완료! 이름: {characterName}, 세대: {generation}대");
}