プレーヤーのステータス④(リトライの仕組みを作る)
リトライの仕組みを作る
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;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public int destroyCount = 0;
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)
{
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★★(追加)
// リトライの命令ブロックを1秒後に呼び出す。
Invoke("Retry", 1.0f);
}
}
}
void UpdatePlayerIcons()
{
for (int i = 0; i < playerIcons.Length; i++)
{
if (destroyCount <= i)
{
playerIcons[i].SetActive(true);
}
else
{
playerIcons[i].SetActive(false);
}
}
}
// ★★(追加)
// ゲームリトライに関する命令ブロック
void Retry()
{
this.gameObject.SetActive(true);
playerHP = 3; // ここの数字は自分が決めたプレーヤーのHP数にすること。
playerHPSlider.value = playerHP;
}
}
Danmaku I(基礎1/全22回)
他のコースを見るリトライの仕組みを作る
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;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public int destroyCount = 0;
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)
{
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
// ★★(追加)
// リトライの命令ブロックを1秒後に呼び出す。
Invoke("Retry", 1.0f);
}
}
}
void UpdatePlayerIcons()
{
for (int i = 0; i < playerIcons.Length; i++)
{
if (destroyCount <= i)
{
playerIcons[i].SetActive(true);
}
else
{
playerIcons[i].SetActive(false);
}
}
}
// ★★(追加)
// ゲームリトライに関する命令ブロック
void Retry()
{
this.gameObject.SetActive(true);
playerHP = 3; // ここの数字は自分が決めたプレーヤーのHP数にすること。
playerHPSlider.value = playerHP;
}
}
プレーヤーのステータス④(リトライの仕組みを作る)