using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class PlayerStat : MonoBehaviour
{
public static PlayerStat instance;//인스턴스
public int character_Lv; //레벨
public int[] needExp;
public int currentEXP; //현재 경험치
public int hp;
public int currentHP;//현재 체력
public int mp;
public int currentMP;//현재 마나
public int atk;//공격력
public int def;//방어력
public int recover_hp;//1초당 회복
public int recover_mp;
public string dmgSound;
public float time;//회복시간
public float current_time;
public GameObject prefabs_Flosting_text;//프리팹
public GameObject parent;//부모 객체
public Slider hpSlider;
public Slider mpSlider;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
instance = this;//인스턴스
currentHP = hp;
currentMP = mp;
current_time = time;
}
public void Hit(int _enemyAtk)
{
int dmg;
if (def >= _enemyAtk)
dmg = 1;
else
dmg = _enemyAtk - def;
currentHP -= dmg;
if (currentHP <= 0)
Debug.Log("체력 0 미만, 게임오버");
AudioManager.instance.Play(dmgSound);
Vector3 vector = this.transform.position;
vector.y += 60;
GameObject clone = Instantiate(prefabs_Flosting_text, vector, Quaternion.Euler(Vector3.zero));
clone.GetComponent<FloatingText>().text.text = dmg.ToString();
clone.GetComponent<FloatingText>().text.color = Color.red;
clone.GetComponent<FloatingText>().text.fontSize = 25;
clone.transform.SetParent(parent.transform);
StartCoroutine(HitCoroutine());
}
IEnumerator HitCoroutine()
{
Color color = GetComponent<SpriteRenderer>().color;
color.a = 0;
GetComponent<SpriteRenderer>().color = color;
yield return new WaitForSeconds(0.1f);
color.a = 1f;
GetComponent<SpriteRenderer>().color = color;
yield return new WaitForSeconds(0.1f);
color.a = 0f;
GetComponent<SpriteRenderer>().color = color;
yield return new WaitForSeconds(0.1f);
color.a = 1f;
GetComponent<SpriteRenderer>().color = color;
yield return new WaitForSeconds(0.1f);
color.a = 0f;
GetComponent<SpriteRenderer>().color = color;
yield return new WaitForSeconds(0.1f);
color.a = 1f;
GetComponent<SpriteRenderer>().color = color;//이 부분작성을 빼놓지 말자 투명에서 유색으로 돌아오지 않는다
}
// Update is called once per frame
void Update()
{
hpSlider.maxValue = hp;
mpSlider.maxValue = mp;
hpSlider.value = currentHP;
mpSlider.value = currentMP;
if (currentEXP >= needExp[character_Lv])//레벨업 함수
{
character_Lv++;
hp += character_Lv * 2;
mp += character_Lv + 2;
currentHP = hp;
currentMP = mp;
atk++;
def++;
}
current_time -= Time.deltaTime;
if(current_time <=0)
{
if(recover_hp > 0)
{
if (currentHP + recover_hp <= hp)
currentHP += recover_hp;
else
currentHP = hp;
}
current_time = time;
}
}
}
이번 시간에는 체력바와 마나바를 만들어 보겠다
Canvas에 빈객체를 만들고 이름은 UI로 한다
그 뒤 그 아래에
UI > Slider 을 눌러서 슬라이더를 만들어주자
그리고 만들어진 슬라이더에서 Handle Slider Area의 아래에 있는 Handie의 이미지 컴포넌트에서 Color의 알파값을 0으로 만들어 준다
슬라이더의 핸들 부분인데 우리는 이걸 안쓴다
그 뒤 슬라이더의 이름을 HPSlider로 바꿔주고 적절한 위치에 배치해준다
그리고 사이즈도 조절해준다
그 뒤 왼쪽의 스크린샷에 있는 HP슬라이더의 이미지를 Fill 오브젝트의 애니메이션을 만들고 애니메이션에 집어넣어준다
그러면 물결처럼 움직이는 듯한 연출을 할 수 있다
다음은 HPSlider을 완성했으니 복재헤서 새로 MPSlider을 만들어 준다
그리고 각 객체의 빨간 슬라이드를 파란 슬라이드 이미지로 바꿔준다
그뒤 애니메이션도 똑같이 MP바가 준비되어 있으니 만들어주자
이제 스크립트를 만지러 가자
PlayerStat 스크립트를 열어보자
슬라이더를 다루려면 using에 UnityEngine.UI를 입력해줘야 한다
빨간색으로 체크한 부분을 작성해주자
변수는 슬라이더 오브젝트를 가져오기 위해 Slider로 hpSlider 로 선언해주었다
hp,mp 둘다 선언해주자
그리고 void Start()에 초기값을 넣어주기 위해
currentHP = hp, currentMP = mp 처럼 작성해준다
그리고 실시간으로 바뀌는 값이기 때문에 void Update에 이렇게 작성해주자
이제 유니티엔진으로 넘어와서
이렇게 새로 만들어진 슬롯에 넣어주고
Hp와 Current HP, Mp와 Current MP의 수치를 넣어준다 이렇게 해주고 테스트를 하면...
이렇게 적에게 대미지를 받으니 플레이어 HP가 깍이면서 구현 되는 것을 볼 수 있다
'쯔꾸르식 유니티 게임 공부' 카테고리의 다른 글
37.바이너리파일 세이브 구현 (0) | 2025.03.15 |
---|---|
36.적 체력바 개별구현 (0) | 2025.03.14 |
34.아이템 장착과 공격 모션 (0) | 2025.03.13 |
33.장비아이템 효과 추가 (0) | 2025.03.11 |
32.장비창 구현 (0) | 2025.03.10 |