欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity开发游戏逻辑:敌人攻击距离

程序员文章站 2024-03-25 23:50:52
...
/***
 * 
 * 把所有存活的敌人放入一个GameObject集合
 * 在一定范围内,遍历集合找出距离最近的敌人
 * 攻击,朝向敌人,对攻击范围内敌人进行伤害处理
 * 
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttrckCtrl : MonoBehaviour
{
    private List<GameObject> _EnemyList;        //当前存活的敌人集合
    private float _MinAttackDis = 1;            //最近攻击距离
    private float _MaxAttackDis = 5;           //最远攻击距离
    private GameObject _NearestEnemy;           //距离最近的敌人

    void Start()
    {
        _EnemyList = new List<GameObject>();
        StartCoroutine("EnemyDetector");                    //协程开启时使用名字,方便关闭
    }

    /// <summary>开启敌人距离检测/// </summary>
    IEnumerator EnemyDetector()
    {
        while (true)
        {
            yield return 0;
            FindAliveEnemy();
            FindNearestEnemy();
        }
    }

    /// <summary>更新存活的敌人/// </summary>
    private void FindAliveEnemy()
    {
        _EnemyList.Clear();
        GameObject[] _Enemys = GameObject.FindGameObjectsWithTag("Enemy");
        for (int i = 0; i < _Enemys.Length; i++)
        {
            if (_Enemys[i].activeInHierarchy)        //敌人处于存活状态
            {
                _EnemyList.Add(_Enemys[i]);
            }
        }
    }

    /// <summary>判断距离最近的存活敌人/// </summary>
    private void FindNearestEnemy()
    {
        if (_EnemyList == null || _EnemyList.Count <= 0) return;

        _NearestEnemy = null;
        float _CurNearestDis = _MaxAttackDis;
        for (int i = 0; i < _EnemyList.Count; i++)
        {
            float _TempDis = CalculationOfDistance(_EnemyList[i].transform.position);
            if (_CurNearestDis > _TempDis)
            {
                _CurNearestDis = _TempDis;
                _NearestEnemy = _EnemyList[i];
            }
        }

        if (_NearestEnemy != null)
        {
            if (InAttackRange(_CurNearestDis))
            {
                //距离最近的敌人在攻击范围内
                Debug.Log("距离最近的敌人 在攻击范围内! 参数: _NearestEnemy = " + _NearestEnemy.name);
            }
        }
    }

    /// <summary>攻击 血值结算/// </summary>
    private void AttackDamageCalculation(GameObject _AttackEnemy)
    {
        if (_AttackEnemy == null) return;

        float _DisOfEnemyAndMe = CalculationOfDistance(_AttackEnemy.transform.position);

        Vector3 _EAndMNormalized = (_AttackEnemy.transform.position - transform.position).normalized;
        float _EAndMDot = Vector3.Dot(transform.forward, _EAndMNormalized);             //判断敌人方位 >0敌人在前方

        if (InAttackRange(_DisOfEnemyAndMe))
        {
            if (_EAndMDot > 0)
            {
                Debug.Log("直接正面发动攻击! 参数: 被攻击的敌人 = " + _AttackEnemy.name);
            }
            else
            {
                Quaternion _Dir = Quaternion.LookRotation(new Vector3(_AttackEnemy.transform.position.x,0, _AttackEnemy.transform.position.z)
                    -new Vector3(transform.position.x, 0, transform.position.z));
                transform.rotation = _Dir;
                Debug.Log("转向发动攻击! 参数: 被攻击的敌人 = " + _AttackEnemy.name);
            }
        }
    }

    /// <summary>
    /// 计算与敌人的距离
    /// </summary>
    /// <param name="v">敌人位置</param>
    /// <returns>距离</returns>
    private float CalculationOfDistance(Vector3 v)
    {
        float _Dis = Vector3.Distance(transform.position, v);
        return _Dis;
    }

    /// <summary>敌人是否攻击范围内/// </summary>
    private bool InAttackRange(float _Dis)
    {
        if (_Dis >= _MinAttackDis && _Dis <= _MaxAttackDis)
        {
            return true;
        }
        return false;
    }

    void OnDestroy()
    {
        StopCoroutine("EnemyDetector");
    }
}


相关标签: Unity3D游戏开发