//攻击 private void TryFire() { //当前时间超过下次攻击时间,开始搜索攻击目标 //当变成范围塔时,m_RigidbodyEnemy失效 if (isFireRange == false) { ResetMinEnemyHealth(); //找到最小血量的敌人 for (int i = 0; i < m_EnemyTriggerList.Count; i++) { m_BasicEnemyTemp = m_EnemyTriggerList[i].GetComponent <BasicEnemy>(); //发现被要求集火的敌人 if (m_BasicEnemyTemp.isFocused == true) { m_BasicEnemyMinHealth = m_BasicEnemyTemp; break; } if (minEnemyHealth > m_BasicEnemyTemp.GetHealth()) { minEnemyHealth = m_BasicEnemyTemp.GetHealth(); m_BasicEnemyMinHealth = m_BasicEnemyTemp; } } //虽然逻辑上不可能进入这个函数后,发现没有敌人 //但是还是保险起见 if (m_BasicEnemyMinHealth != null) { //转向要攻击的目标 m_RigidbodyEnemy = m_BasicEnemyMinHealth.GetComponent <Rigidbody>(); } } //如果达到攻击间隔,开始攻击 if (Time.time > nextFire) { //如果是范围塔,攻击每个进入范围的敌人 if (isFireRange) { for (int i = 0; i < m_EnemyTriggerList.Count; i++) { m_BasicEnemyTemp = m_EnemyTriggerList[i].GetComponent <BasicEnemy>(); Fire(m_BasicEnemyTemp); } m_RangeParticles.GetComponent <ParticleSystem>().Stop(); m_RangeParticles.GetComponent <ParticleSystem>().Play(); m_Animator.SetTrigger("Attack"); } else { //虽然逻辑上不可能进入这个函数后,发现没有敌人 //但是还是保险起见 if (m_BasicEnemyMinHealth != null) { Fire(m_BasicEnemyMinHealth); } } nextFire = Time.time + fireRateSpendSec;//计算下一次攻击时间 } }