リスタート時、数秒だけ無敵状態にする
無敵状態
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;
public Slider hpSlider;
public GameObject[] playerIcons;
private int destroyCount = 0;
// ★追加(無敵)
public bool isMuteki = false;
private void Start()
{
hpSlider.maxValue = playerHP;
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
// ★追加(無敵)
// 条件文の中に「&& isMuteki == false」を追加
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");
}
}
}
}
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;
hpSlider.value = playerHP;
// ★追加(無敵)
// 何秒間無敵状態にするかは自由!
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
// ★追加(無敵)
void MutekiOff()
{
isMuteki = false;
}
}
【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る無敵状態
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;
public Slider hpSlider;
public GameObject[] playerIcons;
private int destroyCount = 0;
// ★追加(無敵)
public bool isMuteki = false;
private void Start()
{
hpSlider.maxValue = playerHP;
hpSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
// ★追加(無敵)
// 条件文の中に「&& isMuteki == false」を追加
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");
}
}
}
}
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;
hpSlider.value = playerHP;
// ★追加(無敵)
// 何秒間無敵状態にするかは自由!
isMuteki = true;
Invoke("MutekiOff", 2.0f);
}
// ★追加(無敵)
void MutekiOff()
{
isMuteki = false;
}
}
リスタート時、数秒だけ無敵状態にする