アイテムの作成①(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;
public int playerHP;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public int destroyCount = 0;
public bool isMuteki = false;
void Start()
{
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false)
{
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);
if (destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
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 = 5;
playerHPSlider.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 > 5)
{
playerHP = 5;
}
// HPスライダー
playerHPSlider.value = playerHP;
}
}
HP回復アイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CureItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
private PlayerHealth playerHealth;
private int reward = 3;
void Start()
{
// 「Player」についている「PlayerHealth」スクリプトにアクセスする。
playerHealth = GameObject.Find("Player").GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
// プレーヤーのミサイルで破壊するとHPが回復する
if (other.gameObject.CompareTag("Missile"))
{
// エフェクトを発生させる
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
// 効果音を出す
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// アイテムを画面から消す(破壊する)
Destroy(this.gameObject);
// プレーヤーのHPを自分が指定した量だけ回復させる
playerHealth.AddHP(reward);
}
}
}
Danmaku Ⅱ(基礎2/全24回)
他のコースを見る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;
public int playerHP;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public int destroyCount = 0;
public bool isMuteki = false;
void Start()
{
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false)
{
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);
if (destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
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 = 5;
playerHPSlider.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 > 5)
{
playerHP = 5;
}
// HPスライダー
playerHPSlider.value = playerHP;
}
}
HP回復アイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CureItem : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip getSound;
private PlayerHealth playerHealth;
private int reward = 3;
void Start()
{
// 「Player」についている「PlayerHealth」スクリプトにアクセスする。
playerHealth = GameObject.Find("Player").GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
// プレーヤーのミサイルで破壊するとHPが回復する
if (other.gameObject.CompareTag("Missile"))
{
// エフェクトを発生させる
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
// 効果音を出す
AudioSource.PlayClipAtPoint(getSound, Camera.main.transform.position);
// アイテムを画面から消す(破壊する)
Destroy(this.gameObject);
// プレーヤーのHPを自分が指定した量だけ回復させる
playerHealth.AddHP(reward);
}
}
}
アイテムの作成①(HP回復)