アイテムの作成②(HP回復)



HP回復
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;
// ★改良(最大HP)
// 「public」を「private」に変更
// maxHPを設定(HPの最大値は自由に設定)
private int playerHP;
private int maxHP = 5;
public Slider hpSlider;
public GameObject[] playerIcons;
private int destroyCount = 0;
public bool isMuteki = false;
private void Start()
{
// ★改良(最大HP)
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);
// ★改良(最大HP)
playerHP = maxHP;
hpSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
void MutekiOff()
{
isMuteki = false;
}
// ★追加(HP回復アイテム)
// 「public」を付けること(ポイント)
public void AddHP(int amount)
{
// 「amount」分だけHPを回復させる
playerHP += amount;
// 最大HP以上には回復しないようにする。
if(playerHP > maxHP)
{
playerHP = maxHP;
}
// HPスライダー
hpSlider.value = playerHP;
}
}

HP回復アイテム(クラス承継)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CureItem : Item // 「MonoBehaviour」を「Item」に変更する(これで「Itemクラスを承継」することができます。)
{
private PlayerHealth ph;
private int reward = 3;
private void Start()
{
// 「Player」についている「PlayerHealth」スクリプトにアクセスする。
ph = GameObject.Find("Player").GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
// プレーヤーのミサイルで破壊するとHPが回復する
if (other.gameObject.tag == "Missile")
{
// (重要ポイント)ItemクラスのItemBaseメソッドを呼び出す。
// エフェクト、効果音等はこれで発生します。
base.ItemBase(other.gameObject);
if(ph != null)
{
// プレーヤーのHPを自分が指定した量だけ回復させる
ph.AddHP(reward);
}
}
}
}


【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る


HP回復
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;
// ★改良(最大HP)
// 「public」を「private」に変更
// maxHPを設定(HPの最大値は自由に設定)
private int playerHP;
private int maxHP = 5;
public Slider hpSlider;
public GameObject[] playerIcons;
private int destroyCount = 0;
public bool isMuteki = false;
private void Start()
{
// ★改良(最大HP)
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);
// ★改良(最大HP)
playerHP = maxHP;
hpSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
void MutekiOff()
{
isMuteki = false;
}
// ★追加(HP回復アイテム)
// 「public」を付けること(ポイント)
public void AddHP(int amount)
{
// 「amount」分だけHPを回復させる
playerHP += amount;
// 最大HP以上には回復しないようにする。
if(playerHP > maxHP)
{
playerHP = maxHP;
}
// HPスライダー
hpSlider.value = playerHP;
}
}

HP回復アイテム(クラス承継)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CureItem : Item // 「MonoBehaviour」を「Item」に変更する(これで「Itemクラスを承継」することができます。)
{
private PlayerHealth ph;
private int reward = 3;
private void Start()
{
// 「Player」についている「PlayerHealth」スクリプトにアクセスする。
ph = GameObject.Find("Player").GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
// プレーヤーのミサイルで破壊するとHPが回復する
if (other.gameObject.tag == "Missile")
{
// (重要ポイント)ItemクラスのItemBaseメソッドを呼び出す。
// エフェクト、効果音等はこれで発生します。
base.ItemBase(other.gameObject);
if(ph != null)
{
// プレーヤーのHPを自分が指定した量だけ回復させる
ph.AddHP(reward);
}
}
}
}


アイテムの作成②(HP回復)