プレーヤーのステータス②(HPを画面に表示する)
プレーヤーHPスライダー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★(追加)
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public AudioClip damageSound;
public int playerHP;
// ★(追加)
private Slider playerHPSlider;
// ★(追加)
private void Start()
{
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
// ★(追加)
playerHPSlider.value = playerHP;
Destroy(other.gameObject);
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);
}
}
}
}
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 destroySound;
public AudioClip damageSound;
public int playerHP;
// ★(追加)
private Slider playerHPSlider;
// ★(追加)
private void Start()
{
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile"))
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
// ★(追加)
playerHPSlider.value = playerHP;
Destroy(other.gameObject);
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を画面に表示する)