プレーヤーのステータス③(残機数を画面に表示する)
残機数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// ★追加
using TMPro;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
// ★追加
public int playerCount; // 残機数
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
// ★追加
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★追加
// HPが0になった瞬間に残機数を1つ減らす。
playerCount -= 1;
playerLabel.text = "" + playerCount;
}
}
}
}
【2021版】Danmaku(基礎/全55回)
他のコースを見る残機数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// ★追加
using TMPro;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
// ★追加
public int playerCount; // 残機数
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
// ★追加
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
if(HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★追加
// HPが0になった瞬間に残機数を1つ減らす。
playerCount -= 1;
playerLabel.text = "" + playerCount;
}
}
}
}
プレーヤーのステータス③(残機数を画面に表示する)