반응형
- 코드 작성
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()
{
// 플레이어 사망
}
}
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 클릭하면 점수증가 시스템 간단 구현 (클릭커 게임) (0) | 2023.01.29 |
---|---|
유니티 C# 중복 생성 방지 간단 구현 DontDestroyOnLoad (0) | 2023.01.29 |
유니티 C# 노치 대응하기 SafeArea 에셋 추천 (0) | 2023.01.29 |
유니티 C# 마우스로 오브젝트 클릭 간단 구현 Mouse Ray cast (0) | 2023.01.29 |
유니티 C# 물리 AddForce 간단 사용법 (0) | 2022.12.22 |
유니티 C# 일정거리 범위 안에 목표물 체크 Vector3.Distance (0) | 2022.12.22 |
유니티 C# UI 효과 깜빡이기 간단 구현 SpriteRenderer (0) | 2022.12.22 |
유니티 C# 라인 따라가기 Follow Line (0) | 2022.12.22 |
댓글