プレーヤーのステータス⑤(ゲームオーバーシーンの作成)





リトライorゲームオーバー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
// ★★★追加
using UnityEngine.SceneManagement;
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);
playerCount -= 1;
playerLabel.text = "" + playerCount;
// ★★★追加
// 残機数によって場合分けを行います。
if(playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
}
}



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




リトライorゲームオーバー
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
// ★★★追加
using UnityEngine.SceneManagement;
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);
playerCount -= 1;
playerLabel.text = "" + playerCount;
// ★★★追加
// 残機数によって場合分けを行います。
if(playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
}
}



プレーヤーのステータス⑤(ゲームオーバーシーンの作成)