(スクリプト)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TankHP_2 : MonoBehaviour { public GameObject effectPrefab; public AudioClip sound; private int HP = 50; public Text HPLabel; private void Start() { HPLabel.text = "HP:" + HP; HPLabel.color = Color.green; } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("EnemyShell")) { HP -= 1; if(HP > 40) { HPLabel.color = Color.green; } else if(HP > 30) { HPLabel.color = Color.yellow; } else if(HP > 10) { HPLabel.color = Color.blue; } else { HPLabel.color = Color.red; } HPLabel.text = "HP:" + HP; Destroy(collision.gameObject); GameObject effect = Instantiate(effectPrefab, collision.transform.position, Quaternion.identity); Destroy(effect, 0.5f); AudioSource.PlayClipAtPoint(sound, transform.position); } } }
(結果)
- HPの数に応じて、HPテキストの色が変化すれば成功