(スクリプト)
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class MonsterHP : MonoBehaviour { private int monsterMaxHP = 15; private int monsterHP; public GameObject effectPrefab; public AudioClip sound; public Slider HPSlider; public Text HPlabel; void Start() { monsterHP = monsterMaxHP; HPSlider.maxValue = monsterMaxHP; HPSlider.value = monsterHP; HPlabel.text = "" + monsterHP; } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Stick")) { monsterHP -= 5; if (monsterHP == 0) { SceneManager.LoadScene("GameOver"); } HPSlider.value = monsterHP; HPlabel.text = "" + monsterHP; GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity); AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position); } } // 追加(やりダメージ) private void OnCollisionEnter(Collision collision) { if(collision.gameObject.CompareTag("Yari")) { monsterHP -= 3; HPSlider.value = monsterHP; HPlabel.text = "" + monsterHP; GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity); AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position); if(monsterHP < 0) { SceneManager.LoadScene("GameOver"); } } } }