//寻找范围内的第一个敌人 public BaseEnemy SearchEnemyInScope(Vector3 vector3, float dis) { foreach (KeyValuePair <string, BaseSingleTypeObjectPool> pool in _mutipleTypeObjectPool._objectsDic) { foreach (BaseEnemy baseEnemy in pool.Value._objectList) { if (GameTool.IsObjectInScope(baseEnemy._entityGameObject, vector3, dis)) { return(baseEnemy); } } } return(null); }
protected virtual void Attack(Action <Transform> attackAction = null) { //判断攻击CD _attackTime += Time.deltaTime; if (_attackTime < _attackCDInterval) { return; } //还没有攻击目标 if (_targetEnemy == null) { if (!SearchEnemy()) { return; } } else { //敌人超出攻击范围 马上寻找 if (!GameTool.IsObjectInScope(_targetEnemy._entityGameObject, _entityGameObject.transform.position, _attackDistance)) { _targetEnemy = null; return; //如果需要延长可以去掉寻找敌人部分代码 //if (!SearchEnemy()) //{ // return; //} } } //获取攻击坐标 Transform targeTransform = _targetEnemy._entityGameObject.transform; //炮口转向目标 float ty = _rotateObject.position.y; Vector3 tv3 = targeTransform.position; _rotateObject.LookAt(new Vector3(tv3.x, ty, tv3.z)); //产生攻击特效 attackAction(targeTransform); //刷新攻击时间 _attackTime = 0; }
//寻找范围内的第一个敌人 public BaseEnemy SearchRandomEnemyInScope(Vector3 vector3, float dis) { List <BaseEnemy> tempEnemys = new List <BaseEnemy>(); foreach (KeyValuePair <string, BaseSingleTypeObjectPool> pool in _mutipleTypeObjectPool._objectsDic) { foreach (BaseEnemy baseEnemy in pool.Value._objectList) { if (GameTool.IsObjectInScope(baseEnemy._entityGameObject, vector3, dis)) { tempEnemys.Add(baseEnemy); } } } if (tempEnemys.Count == 0) { return(null); } int n = Random.Range(0, tempEnemys.Count); return(tempEnemys[n]); }