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

在开发游戏的过程中,我们常常会出现一些敌人攻击我们玩家,并且实现掉血以及死亡的现象,敌人还会源源不断地生成,这是怎么制作的呢,接下来为大家提供方法。其中使用了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--;
    }
}

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

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nicole Potter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值