반응형
- 코드 작성
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public int health = 3;
public Image[] healthImages;
public void TakeDamage() //공격하기
{
health -= 1;
healthImages[health].gameObject.SetActive(false);
Debug.Log("Player's health: " + health);
}
}
- 다른 쪽에서 플레이어 공격할때
Player player = new Player();
player.TakeDamage();
- 유니티 기본 UI Slider를 사용하여 구현하기
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public Slider healthSlider;
void Start()
{
currentHealth = maxHealth;
healthSlider.value = currentHealth;
}
public void TakeDamage(int damage)
{
currentHealth = Mathf.Max(currentHealth - damage, 0);
healthSlider.value = currentHealth;
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
// 플레이어 사망
}
}
반응형