基于 项目02《游戏-13-开发》Unity3D ,
任务:战斗系统之击败怪物与怪物UI血条信息

using UnityEngine;
 public abstract class Living : MonoBehaviour{
     protected float hp;
     protected float attack;
     protected float define;
     protected bool isDead;
     public float Hp {
         get {
             return hp;
         }
         set {
             hp = value;
         }
     }
     public Animator Anim { get; set; }
     public virtual void onHurt(float attack) {
         float lost = attack - this.define;
         if (lost <= 0)
             return;
         this.hp -= lost;
         if (this.hp < 0) {
             this.isDead = true;
             //播放死亡动画
             Anim.SetTrigger("DeathTrigger");
             if (isDead == true)
                 return;
         }
     }
     protected void Start(){
         InitValue();
     }
     protected virtual void InitValue() {
         this.hp = 10;
         this.attack = 3;
         this.define = 1;
         this.isDead = false;
         Anim = GetComponent<Animator>();
     }
 }

using UnityEngine;
 public class Enemy : Living{
     protected override void InitValue(){
         this.hp = 30;
         this.attack = 3;
         this.define = 1;
         this.isDead = false;
         Anim = GetComponent<Animator>();
     }
 }

using UnityEngine;
 public class Weapon : MonoBehaviour{
     float damage = 3f;
     public Animator Anim { get; set; }
     bool isdead;
     void Awake()
     {
         isdead = false;
         Anim = GetComponent<Animator>();
     }
     void OnTriggerEnter(Collider other)
     {
         Debug.Log("怪物碰撞武器");
         if (other.CompareTag("Enemy"))
         {
             Enemy enemy = other.GetComponent<Enemy>();
             if (enemy != null)
             {
                 if (enemy.Hp <= 0)
                 {
                     if (isdead == true)
                         return;
                     isdead = true;
                     enemy.GetComponent<Animator>().SetTrigger("DeathTrigger");
                 }
                 enemy.Hp -= damage;
                 enemy.GetComponent<Animator>().SetTrigger("HitTrigger");
             }
         }
     }
 }

using UnityEngine;
 using UnityEngine.UI;
 public class EnemyInfo : MonoBehaviour{
     Enemy enemy;
     Slider health1;
     void Start(){
         var enemyObj = GameObject.FindGameObjectWithTag("Enemy");
         enemy = enemyObj.GetComponent<Enemy>();
         health1 = GameObject.Find("H/Slider3").GetComponent<Slider>();
     }
     void Update(){
         if (health1 != null)
             health1.value = enemy.Hp;
     }
 }

using UnityEngine;
 public class Cage : MonoBehaviour{
     private GameObject uiPrefabInstance; //敌人信息实例化UI
     void OnTriggerEnter(Collider other){
         Debug.Log("角色进入牢笼");
         if (other.CompareTag("Player")){
             GameObject prefab = Resources.Load<GameObject>("Prefabs/Panel/Package/EnemyInfo");
             uiPrefabInstance = Instantiate(prefab, new Vector3(0f, -100, 0f), Quaternion.identity);
             uiPrefabInstance.transform.SetParent(GameObject.Find("Canvas").transform, false);
         }
     }
     private void OnTriggerExit(Collider other){
         if (other.CompareTag("Player"))
             GameObject.Find("EnemyInfo(Clone)").gameObject.SetActive(false);
     }
 }
实现效果:
 进入牢笼后,
进入牢笼后,

退出牢笼后,

击打怪物,
 当血量减为0,
当血量减为0,

离开牢笼,

End.