ミッション失敗時の流れの作成


ミッション失敗時の流れ
using UnityEngine;
using TMPro;
public class TankHP : MonoBehaviour
{
public int HP;
public TextMeshProUGUI HPLabel;
private int maxHP = 8;
public AudioClip itemSound;
public GameObject effectPrefab;
// ★追加
public GameObject failedCanvas;
private void Start()
{
HPLabel.text = "" + HP;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("EnemyShell"))
{
HP -= 1;
HPLabel.text = "" + HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
// ★追加
failedCanvas.SetActive(true);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("HPItem"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(itemSound, transform.position);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
HP += 2;
HPLabel.text = "" + HP;
if (HP > maxHP)
{
HP = maxHP;
HPLabel.text = "" + HP;
}
}
}
}





【Unity6版】BattleTank(全31回)
他のコースを見る

ミッション失敗時の流れ
using UnityEngine;
using TMPro;
public class TankHP : MonoBehaviour
{
public int HP;
public TextMeshProUGUI HPLabel;
private int maxHP = 8;
public AudioClip itemSound;
public GameObject effectPrefab;
// ★追加
public GameObject failedCanvas;
private void Start()
{
HPLabel.text = "" + HP;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("EnemyShell"))
{
HP -= 1;
HPLabel.text = "" + HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
// ★追加
failedCanvas.SetActive(true);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("HPItem"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(itemSound, transform.position);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
HP += 2;
HPLabel.text = "" + HP;
if (HP > maxHP)
{
HP = maxHP;
HPLabel.text = "" + HP;
}
}
}
}





ミッション失敗時の流れの作成