项目02《游戏-14-开发》Unity3D

文章详细描述了在Unity3D中如何实现战斗系统,包括Living类的抽象,Enemy和Weapon组件的行为,以及EnemyInfo和Cage组件用于显示怪物血条和在牢笼内的交互。怪物被击败时会触发相应的动画效果。
摘要由CSDN通过智能技术生成

基于      项目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,

离开牢笼,

End.

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值