游戏开发常见操作系列之敌人系统的开发一(U3D)

2024-01-21

在开发游戏的过程中,我们常常会出现一些敌人攻击我们玩家,并且实现掉血以及死亡的现象,敌人还会源源不断地生成,这是怎么制作的呢,接下来为大家提供方法。其中使用了NGUI,后续会更新其它方法,敬请期待!

使用HUDText实现扣血时显示文本,直接使用让开发更方便。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HUDTextParent : MonoBehaviour
{
    public static HUDTextParent _instance;
    private void Awake()
    {
        _instance = this; 
    }
}

敌人的逻辑:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum WolfState
{
    Idle,
    Walk,
    Attack,
    Death
}
public class WolfBaby : MonoBehaviour
{
    public WolfState state = WolfState.Idle;
    public string aniname_death;
    public string aniname_idle;
    public string aniname_walk;
    public string aniname_now;
    public float time = 1;
    private float timer = 0;
    public float speed = 1;
    public CharacterController cc;
    public int hp = 100;
    public int exp = 10;
    public int attack = 10;
    public float miss_rate = 0.2f;

    public AudioClip miss_sound;

    
    private bool is_attacked = false;
    private Color normal;
    private GameObject hudtextFollow;
    private GameObject hudtextGo;
    public GameObject hudtextPrefab;
    public HUDText hudtext;
    public UIFollowTarget followTarget;
    public GameObject body;

    public string aniname_normalattack;
    public float time_normalattack;

    public string aniname_crayattack;
    public float time_crayattack;
    public float crazyattack_rate;

    public string aniname_attack_now;
    public int attack_rate;//攻击速率 每秒
    private float attack_timer = 0;

    public Transform target;

    //巡逻的距离
    public float minDistance = 3;
    public float maxDistance = 8;

    public WolfSpawn spawn;
    //text
    public PlayerStatus ps;
    public string WolfType;
   

    // Start is called before the first frame update
    private void Awake()
    {
        aniname_now = aniname_idle;
        // cc = this.GetComponent<CharacterController>();
       // body = transform.Find("Wolf_Baby").gameObject;
        normal = this.transform.Find(WolfType). GetComponent<Renderer>().material.color;
        hudtextFollow = transform.Find("HUDText").gameObject;
    }
    void Start()
    {
        //hudtextGo=GameObject.Instantiate(hudtextPrefab,Vector3.zero, Quaternion.identity);
        //hudtextGo.transform.parent = HUDTextParent._instance.gameObject.transform;
        hudtextGo = NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab);
        hudtext=hudtextGo.GetComponent<HUDText>();
        followTarget=hudtextGo.GetComponent<UIFollowTarget>();
         followTarget.target = hudtextFollow.transform;

        followTarget.gameCamera = Camera.main;

       ps=GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
      
      //  followTarget.uiCamera = UICamera.current.GetComponent<Camera>();
    }

    //自动攻击
    void AutoAttack()
    {
        if(target!=null)
        {
            PlayerState playerState = target.GetComponent<PlayerAttack>().state;
            if(playerState==PlayerState.Death)
            {
                target = null;
                state = WolfState.Idle;
                return;
            }
            float distance = Vector3.Distance(target.position, transform.position);
            if(distance>maxDistance)
            {
                //停止自动攻击
                target = null;
                state = WolfState.Idle;
            }
            else if(distance<=minDistance)
            {
                //自动攻击
                attack_timer += Time.deltaTime;
                GetComponent<Animation>().CrossFade(aniname_attack_now);
               
                if(aniname_attack_now==aniname_normalattack)
                {
                   
                    if (attack_timer>time_normalattack)
                    {
                        
                        //产生伤害
                        target.GetComponent<PlayerAttack>().TakeDamage(attack);
                       
                        aniname_attack_now = aniname_idle;
                    }
                }
                else if(aniname_attack_now==aniname_crayattack)
                {
                    if(attack_timer>time_crayattack)
                    {
                        //产生伤害
                        target.GetComponent<PlayerAttack>().TakeDamage(attack);
                        
                        aniname_attack_now = aniname_idle;
                    }
                }
                if(attack_timer>(1f/attack_rate))
                {
                    RandomAttack();
                    attack_timer = 0;
                }
            }
            else
            {
                //朝向角色移动
                transform.LookAt(target);
                cc.SimpleMove(transform.forward * speed);
                GetComponent<Animation>().CrossFade(aniname_walk);
            }
        }
        else
        {
            state = WolfState.Idle;
        }

    }

    //随机发起攻击
    void RandomAttack()
    {
        float value = Random.Range(0f, 1f);
        if(value<crazyattack_rate)
        {
            //进行疯狂攻击
            aniname_attack_now = aniname_crayattack;
        }
        else
        {
            //进行普通攻击
            aniname_attack_now = aniname_normalattack;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if(state==WolfState.Death)
        {
            //死亡
            GetComponent<Animation>().CrossFade(aniname_death);
        }
        else if(state==WolfState.Attack)
        {
            //自动攻击状态
            //TODO
            AutoAttack();
        }
        else
        {
            //巡逻
            GetComponent<Animation>().CrossFade(aniname_now);
            if(aniname_now==aniname_walk)
            {
                cc.SimpleMove(transform.forward * speed);
            }
            timer += Time.deltaTime;
            if(timer>=time)
            {
                timer = 0;
                RandomState();
            }
        }

        //FOR TEXT
        //if(Input.GetMouseButton(1))
        //{
        //    TakeDamage(1);
        //}
    }
    void RandomState()
    {
        int value = Random.Range(0, 2);
        if(value==0)
        {
            aniname_now = aniname_idle;
        }
        else
        {
            if (aniname_now != aniname_walk)
            {
                transform.Rotate(transform.up * Random.Range(0, 360));
            }
            aniname_now = aniname_walk;
        }
    }
    public void TakeDamage(int attack)
    {
        //受到伤害
        if (state == WolfState.Death) return;
        target = GameObject.FindGameObjectWithTag(Tags.player).transform;
        state=WolfState.Attack;
        float value = Random.Range(0f, 1f);
        if(value<miss_rate)
        {
            //Miss效果
            AudioSource.PlayClipAtPoint(miss_sound, transform.position);
            hudtext.Add("Miss", Color.gray, 1);
        }
        else
        {
            //打中效果
            hudtext.Add("-"+attack,Color.red, 1);
            this.hp -= attack;
            StartCoroutine(ShowBodyRed());
            if(hp<=0)
            {
                state = WolfState.Death;
                Destroy(this.gameObject, 2);
            }
        }
    }
    IEnumerator ShowBodyRed()
    {
       body. GetComponent<Renderer>().material.color = Color.red;
        yield return new WaitForSeconds(1f);
        body.GetComponent<Renderer>().material.color = normal;
    }

    //销毁的方法
    private void OnDestroy()
    {
        spawn.MinusNumber(); 
        ps.GetExp(exp);
       
        BarNPC._instance.OnKillWolf();
        GameObject.Destroy(hudtextGo);
    }

    private void OnMouseEnter()
    {
        if (PlayerAttack._instance.isLockingTarget == false)
        {
            CursorManager._instance.SetAttack();
        }
    }
    private void OnMouseExit()
    {
        if (PlayerAttack._instance.isLockingTarget == false)
        {
            CursorManager._instance.SetNormal();
        }
    }
}

敌人生成的逻辑(你可以理解为敌人窝,用来产生敌人的)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WolfSpawn : MonoBehaviour
{
    public int maxnum = 7;
    private int currentnum = 0;
    public float time = 3;
    private float timer = 0;
    public GameObject prefab;
    void Update()
    {
        if(currentnum<maxnum)
        {
            timer += Time.deltaTime;
            if(timer>time)
            {
                Vector3 pos = transform.position;
                pos.x += Random.Range(-5, 5);
                pos.z += Random.Range(-5, 5);
                GameObject go=GameObject.Instantiate(prefab, pos, Quaternion.identity);
                go.GetComponent<WolfBaby>().spawn = this;
                timer = 0;
                currentnum++;
            }
        }
    }

    public void MinusNumber()
    {
        this.currentnum--;
    }
}

如果你直接复制我的代码发现报错,那可能是因为我的代码关联了其它系统,你可以查看我的其它笔记了解其它的系统,或者直接修改一些操作,核心部分全在这里了,希望对你有所帮助,点个赞支持一下吧!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

游戏开发常见操作系列之敌人系统的开发一(U3D) 的相关文章

随机推荐

  • 打造完美人像,PixCake像素蛋糕助您一键修图

    您是否曾经为自己的人像照片需要进行繁琐的修图而感到困扰 是否曾经想要打造出完美的自拍照 却不知道该如何下手 现在 我们为您推荐一款强大的人像处理技术修图软件 PixCake像素蛋糕 PixCake像素蛋糕是一款基于AI人像处理技术的修图软件
  • 【计算机毕业设计】springbootstone音乐播放器的设计与实现

    随着我国经济的高速发展与人们生活水平的日益提高 人们对生活质量的追求也多种多样 尤其在人们生活节奏不断加快的当下 人们更趋向于足不出户解决生活上的问题 stone音乐播放器展现了其蓬勃生命力和广阔的前景 与此同时 为解决用户需求 stone
  • RubyMine for Mac/win:提升Ruby和Rails开发的强大IDE

    随着Ruby和Rails在Web开发领域的广泛应用 一款高效的开发工具对于提高生产力至关重要 JetBrains RubyMine正是这样一款值得信赖的集成开发环境 IDE 作为Mac和Windows平台上的强大工具 RubyMine为开发
  • U3D游戏开发中摇杆的制作(UGUI版)

    在PC端模拟摇杆 实现玩家通过控制摇杆让玩家移动 以下是完整代码 using System Collections using System Collections Generic using UnityEngine using Unity
  • U3D游戏开发中摇杆的制作(NGUI版)

    在PC端模拟摇杆 实现控制摇杆让玩家或者物体移动 以下是完整代码 using System Collections using System Collections Generic using UnityEngine public clas
  • 游戏开发常见操作梳理之NPC任务系统

    多数游戏存在任务系统 接下来介绍通过NPC触发任务的游戏制作代码 using System Collections using System Collections Generic using UnityEngine
  • 游戏开发创建操作之玩家信息系统的建立

    游戏一般都需要玩家信息系统 那么我们应该如何搭建玩家信息系统 接下来我将展示一种简单的方法 完整代码如下 using System Collections using System Collections Generic using Uni
  • iStat Menus:Mac用户的系统状态守护者

    iStat Menus 一款为Mac用户精心设计的系统状态监控工具 致力于为用户提供关于系统性能 硬件状态和网络活动的实时信息 它不仅是一个监控工具 更是一个守护者 始终守护着Mac系统的安全与稳定 通过直观的仪表盘风格 iStat Men
  • 游戏开发常见操作梳理系列之——玩家信息的显示系统

    在游戏中 有不少游戏在左上角会出现玩家的头像和等级以及血量 这就是玩家的信息显示系统 那么这些是如何制作的呢 接下来我将讲讲代码的操作 其它操作我会在其它笔记中一一说明 敬请期待 信息的显示相当简单就是控制一些UI 然后在其它系统里面填写相
  • 提升编程效率,Sublime Text 4 for Mac 让代码编辑更高效!

    作为一名开发人员或程序员 一个高效且功能强大的文本编辑器是必不可少的工具 而 Sublime Text 4 for Mac 正是为满足这一需求而设计的 无论你是初学者还是经验丰富的专业人士 Sublime Text 4 都将成为你编程生涯中
  • 游戏开发常见操作梳理之NPC药品商店系统(NGUI版)

    后续会出UGUI Json的版本 敬请期待 游戏开发中经常会出现药品商店 实际操作与武器商店类似 甚至根据实际情况可以简化设置 废话不多说 直接上代码 药品商店的源码 using System Collections using Syste
  • 揭秘网络世界的幕后密码——Wireshark网络协议分析软件

    在我们日常生活中 计算机和互联网已经成为不可或缺的一部分 然而 很少有人真正了解网络背后复杂的工作原理和通信协议 幸运的是 有一款强大而实用的软件 Wireshark 可以帮助我们深入了解网络世界的幕后密码 Wireshark是一款免费的网
  • 解锁思维潜能,畅享XMind 2024 Mac/win中文版思维导图软件

    XMind 2024是一款功能强大的思维导图软件 旨在帮助用户提高工作效率和组织思维 它的核心特点包括多平台同步 强大的协作功能和丰富的导图模板 首先 XMind 2024支持多平台的无缝同步 用户可以在电脑 手机和平板上随时随地访问和编辑
  • 游戏开发常见操作梳理之小地图的制作

    游戏中一般存在小地图系统 实际上就是设置一个新的摄像机放置在玩家的正上方 然后在小地图上显示新摄像机看见的东西就可以了 在小地图上一般存在放大地图和缩小地图的按钮可以方便放大和缩小地图 这些操作是如何实现的呢 接下来直接上核心代码 usin
  • 用Growly Draw for Mac,释放您的创意绘画天赋!

    在数字化时代 绘画已经不再局限于传统的纸笔之中 如今 我们可以借助强大的绘画应用软件 将创意化为独特的艺术作品 而Growly Draw for Mac就是一款让您能够快速释放创意 创作精美绘画作品的应用软件 Growly Draw for
  • 游戏开发之常见操作梳理——武器装备商店系统(NGUI版)

    游戏开发中经常出现武器商店 接下来为你们带来武器装备商店系统的具体解决办法 后续出UGUI Json版本 敬请期待 武器道具的具体逻辑 using System Collections using System Collections Ge
  • 游戏开发常见操作梳理之角色选择一

    进入游戏后 我们经常会进入角色选择的界面 通常是左右两个按钮可以更改角色供玩家选择 对于这种界面我们通常使用数据持久化将角色信息存储起来 接下来的笔记中 我将使用自带的数据持久化系统对其进行操作 实现角色的选择页面 后续会更新xml系列的文
  • Effective C++——尽可能使用const

    const允许指定一个语义约束 也就是指定一个 不该被改动 的对象 而编译器会强制实施这项约束 只要保持某个值不变是事实 就应该说出来 以获得编译器的协助 保证不被违反 const与指针 注意const的写法 const char p p可
  • 游戏开发常用实践操作之按动任意键触发

    接下来一些笔记会对于一些大大小小的实践操作进行记录 希望对你有所帮助 在游戏中 我们经常会遇到一些按动任意键触发的操作 接下来展示核心代码 以下是对于Unity中的操作 使用的UI是NGUI 对于核心操作没有影响 你可以自己置换 void
  • 游戏开发常见操作系列之敌人系统的开发一(U3D)

    在开发游戏的过程中 我们常常会出现一些敌人攻击我们玩家 并且实现掉血以及死亡的现象 敌人还会源源不断地生成 这是怎么制作的呢 接下来为大家提供方法 其中使用了NGUI 后续会更新其它方法 敬请期待 使用HUDText实现扣血时显示文本 直接