プレーヤーのステータス②(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 maxHP;
private int HP;
// ★追加
public Slider HPSlider;
void Start()
{
HP = maxHP;
// ★追加
HPSlider.maxValue = HP; // 最大値
HPSlider.value = HP;
}
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);
}
}
}
}



【2021版】Danmaku(基礎/全55回)
他のコースを見る







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 maxHP;
private int HP;
// ★追加
public Slider HPSlider;
void Start()
{
HP = maxHP;
// ★追加
HPSlider.maxValue = HP; // 最大値
HPSlider.value = HP;
}
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スライダーの作成)