複数ステージ⑤(残機数データを引き継ぐ)
![75173394 1169 454d 8bec 57648958b19e](https://codegenius.org/uploads/slide/image/4890/75173394-1169-454d-8bec-57648958b19e.jpeg)
![7894c5b2 cbc2 4523 94b8 108d2f0e2dcb](https://codegenius.org/uploads/slide/image/4891/7894c5b2-cbc2-4523-94b8-108d2f0e2dcb.jpeg)
![9008344f d58c 44f0 ac44 1620323495bc](https://codegenius.org/uploads/slide/image/4892/9008344f-d58c-44f0-ac44-1620323495bc.jpeg)
残機数を引き継ぐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
private int playerHP;
private int maxHP = 5;
public Slider hpSlider;
public GameObject[] playerIcons;
public bool isMuteki = false;
// ★修正
public static int destroyCount = 0;
private void Start()
{
// ★追加
// この1行を追加しないと、残機数データと残機数の表示(アイコン数)がずれてしまう。
UpdatePlayerIcons();
playerHP = maxHP;
hpSlider.maxValue = playerHP;
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false)
{
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);
destroyCount += 1;
UpdatePlayerIcons();
if(destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
destroyCount = 0;
}
}
}
}
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 = maxHP;
hpSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
void MutekiOff()
{
isMuteki = false;
}
public void AddHP(int amount)
{
playerHP += amount;
if(playerHP > maxHP)
{
playerHP = maxHP;
}
hpSlider.value = playerHP;
}
public void Player1UP(int amount)
{
destroyCount -= amount;
if(destroyCount < 0)
{
destroyCount = 0;
}
UpdatePlayerIcons();
}
}
![964f1fb1 eeaf 4373 84a7 d988fb014bd3](https://codegenius.org/uploads/slide/image/4893/964f1fb1-eeaf-4373-84a7-d988fb014bd3.jpeg)
【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る![75173394 1169 454d 8bec 57648958b19e](https://codegenius.org/uploads/slide/image/4890/75173394-1169-454d-8bec-57648958b19e.jpeg)
![7894c5b2 cbc2 4523 94b8 108d2f0e2dcb](https://codegenius.org/uploads/slide/image/4891/7894c5b2-cbc2-4523-94b8-108d2f0e2dcb.jpeg)
![9008344f d58c 44f0 ac44 1620323495bc](https://codegenius.org/uploads/slide/image/4892/9008344f-d58c-44f0-ac44-1620323495bc.jpeg)
残機数を引き継ぐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
private int playerHP;
private int maxHP = 5;
public Slider hpSlider;
public GameObject[] playerIcons;
public bool isMuteki = false;
// ★修正
public static int destroyCount = 0;
private void Start()
{
// ★追加
// この1行を追加しないと、残機数データと残機数の表示(アイコン数)がずれてしまう。
UpdatePlayerIcons();
playerHP = maxHP;
hpSlider.maxValue = playerHP;
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false)
{
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);
destroyCount += 1;
UpdatePlayerIcons();
if(destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
destroyCount = 0;
}
}
}
}
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 = maxHP;
hpSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
void MutekiOff()
{
isMuteki = false;
}
public void AddHP(int amount)
{
playerHP += amount;
if(playerHP > maxHP)
{
playerHP = maxHP;
}
hpSlider.value = playerHP;
}
public void Player1UP(int amount)
{
destroyCount -= amount;
if(destroyCount < 0)
{
destroyCount = 0;
}
UpdatePlayerIcons();
}
}
![964f1fb1 eeaf 4373 84a7 d988fb014bd3](https://codegenius.org/uploads/slide/image/4893/964f1fb1-eeaf-4373-84a7-d988fb014bd3.jpeg)
複数ステージ⑤(残機数データを引き継ぐ)