AttackEnemy人物攻击判断

时间:2021-09-10 09:06:22

AttackEnemy人物攻击判断

/// <param name="attackArea">攻击范围</param>

/// <param name="attackPowerMultiple">攻击力度(倍率)</param>

/// <param name="isDirection">攻击是否有方向性</param>

protected void AttackEnemy(List<GameObject> lisEnemys, Transform traNearestEnemy, float attackArea, int attackPowerMultiple, bool isDirection = true)

{

//参数检查

if (lisEnemys == null || lisEnemys.Count <= 0)

{

traNearestEnemy = null;

return;

}

foreach (GameObject enemyItem in lisEnemys)

{

if (enemyItem && enemyItem.GetComponent<Ctrl_BaseEnemyProperty>())

{

if (enemyItem.GetComponent<Ctrl_BaseEnemyProperty>().CurrentState != EnemyState.Death)

{

//敌我距离

float floDistance = Vector3.Distance(this.gameObject.transform.position, enemyItem.transform.position);

//攻击具有方向性

if (isDirection)

{

//定义“主角与敌人” 的方向

        Vector3 dir = (enemyItem.transform.position - this.gameObject.transform.position).normalized;

//定义“主角与敌人”的夹角(用向量的“点乘”进行计算)

        float floDirection = Vector3.Dot(dir, this.gameObject.transform.forward);

        //如果主角与敌人在同一个方向,且在有效攻击范围内,则对敌人做伤害处理

if (floDirection > 0 && floDistance <= attackArea)

{

          enemyItem.SendMessage("OnHurt", Ctrl_HeroProperty.Instance.GetCurrentATK() * attackPowerMultiple, SendMessageOptions.DontRequireReceiver);

}

}

//攻击无方向性

else

{

if (floDistance <= attackArea)

{

          enemyItem.SendMessage("OnHurt", Ctrl_HeroProperty.Instance.GetCurrentATK() * attackPowerMultiple, SendMessageOptions.DontRequireReceiver);

}

}

}

}

}

}

注:此方法根据方向、距离进行播放不同的动画,来完成攻击,加入伤害后使用、具体套用方法请看后续代码(如有错误或运行Bug、请联系本人)...