プレーヤーのステータス②(HPを画面に表示する)
HPスライダーの設定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
// ★追加
public Slider hpSlider;
// ★追加
private void Start()
{
// スライダーの最大値の設定
hpSlider.maxValue = playerHP;
// スライダーの現在値の設定
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
// ★追加
// スライダーの現在値
hpSlider.value = playerHP;
if (playerHP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
}
}
}
}
【2019版】Danmaku I(基礎1/全22回)
他のコースを見るHPスライダーの設定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
// ★追加
public Slider hpSlider;
// ★追加
private void Start()
{
// スライダーの最大値の設定
hpSlider.maxValue = playerHP;
// スライダーの現在値の設定
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
// ★追加
// スライダーの現在値
hpSlider.value = playerHP;
if (playerHP == 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を画面に表示する)