当前位置: 首页 > news >正文

项目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.lryc.cn/news/298370.html

相关文章:

  • 【Java数据结构】单向 不带头 非循环 链表实现
  • 【ES6】模块化
  • 腾讯云4核8G服务器可以用来干嘛?怎么收费?
  • 怎么在bash shell中操作复杂json对象
  • 11.div函数
  • windows11 MSYS2下载安装教程
  • Excel+VBA处理高斯光束
  • 如何启动若依框架
  • 案例:CentOS8 在 MySQL8.0 实现半同步复制
  • 阿里云带宽计费模式怎么选?如何收费的?
  • c#记录几个问题
  • 第69讲后端登录逻辑实现
  • Qt 字符串类应用与常用基本数据类型
  • JAVA面试题15
  • git安装及使用
  • 电力负荷预测 | Matlab实现基于LSTM长短期记忆神经网络的电力负荷预测模型(结合时间序列)
  • 力扣:455. 分发饼干
  • SpringCloud-项目引入Nacos
  • 如何在 Windows 10/11 上恢复回收站永久删除的文件夹?
  • 七、滚动条操作——调整图像对比度
  • 免费生成ios证书的方法(无需mac电脑)
  • gtkmm4 应用程序使用 CSS 样式
  • 科研绘图-半小提琴图-
  • 机器学习 | 深入集成学习的精髓及实战技巧挑战
  • SNMP(简单网络管理协议)介绍
  • Spring中常见的设计模式
  • 【MySQL】——数值函数的学习
  • LLMs模型选择,LLMs复读机问题,LLMs长文本处理方案
  • LeetCode.144. 二叉树的前序遍历
  • Redis复制