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

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


http://www.niftyadmin.cn/n/5376440.html

相关文章

【漏洞复现】狮子鱼CMS文件上传漏洞(wxapp.php)

Nx01 产品简介 狮子鱼CMS&#xff08;Content Management System&#xff09;是一种网站管理系统&#xff0c;它旨在帮助用户更轻松地创建和管理网站。该系统拥有用户友好的界面和丰富的功能&#xff0c;包括页面管理、博客、新闻、产品展示等。通过简单直观的管理界面&#xf…

PhP+vue企业原材料采购系统_cxg0o

伴随着我国社会的发展&#xff0c;人民生活质量日益提高。互联网逐步进入千家万户&#xff0c;改变传统的管理方式&#xff0c;原材料采购系统以互联网为基础&#xff0c;利用php技术&#xff0c;结合vue框架和MySQL数据库开发设计一套原材料采购系统&#xff0c;提高工作效率的…

with 用法

with 已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它&#xff0c;但也许已从相关的 web 标准中移除&#xff0c;也许正准备移除或出于兼容性而保留。请尽量不要使用该特性&#xff0c;并更新现有的代码&#xff1b;参见本页面底部的兼容性表格以指导你作出决定。请注意…

vue前端系统启动报错Module not found: Error: Can‘t resolve ‘sass-loader‘

1、确认项目中是否已安装 node-sass 包。sass-loader 是依赖于 node-sass 包的&#xff0c;如果没有安装 node-sass 包&#xff0c;也会导致无法找到 sass-loader 包。 npm ls node-sass安装 node-sass 包&#xff1a; npm install --save-dev node-sass2、确认项目中是否已安…

【数据结构】二叉树的顺序结构及链式结构

目录 1.树的概念及结构 1.1树的概念 1.2树的相关概念 ​编辑 1.3树的表示 1.4树在实际中的运用&#xff08;表示文件系统的目录树结构&#xff09; 2.二叉树概念及结构 2.1二叉树的概念 2.2现实中的二叉树 ​编辑 2.3特殊的二叉树 2.4二叉树的性质 2.5二叉树的存储结…

容器高级知识: 适配器模式与 Sidecar 模式的区别

适配器模式与 Sidecar 模式的区别 在 Kubernetes 中&#xff0c;适配器模式和 Sidecar 模式都是扩展您的主应用程序容器功能的方法&#xff0c;但它们具有不同的目的和功能&#xff1a; Sidecar 模式&#xff1a; 通用目的&#xff1a; 为主应用程序提供 补充功能&#xff0…

Kubernetes与Docker的深入对比:解析容器编排与容器引擎的区别与联系

Kubernetes与Docker的深入对比&#xff1a;解析容器编排与容器引擎的区别与联系 引言 容器技术的崛起为软件开发和部署带来了革命性的变化。在这个领域&#xff0c;Kubernetes&#xff08;简称K8s&#xff09;和Docker是两个备受瞩目的技术&#xff0c;但它们之间有着明显的区…

数据库被人破解,删除数据,勒索

事情是这样的&#xff0c;我买了一台服务器自己部署项目玩儿玩儿&#xff0c;我的数据库运行在3306端口&#xff0c;密码没改&#xff0c;就是默认的123456&#xff0c;诡异的事情发生了&#xff0c;用了一段时间之后&#xff0c;数据库突然连接不上了&#xff0c;我一通操作猛…