53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class ArcherHunterSkill : UnitSkill
|
|
{
|
|
[Min(0f)]
|
|
public float splitShotRadius = 3f;
|
|
|
|
public override void ReleaseSkill()
|
|
{
|
|
GameObject mainTarget = unit.Target;
|
|
if (mainTarget == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameObject nearbyOpponent = FindNearbyOpponent(mainTarget);
|
|
unit.FireSkillProjectile(nearbyOpponent != null ? nearbyOpponent : mainTarget);
|
|
}
|
|
|
|
private GameObject FindNearbyOpponent(GameObject mainTarget)
|
|
{
|
|
UnitTeam myTeam = unit.GetComponent<UnitTeam>();
|
|
if (myTeam == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
GameObject nearbyOpponent = null;
|
|
float nearestSqrDistance = splitShotRadius * splitShotRadius;
|
|
foreach (UnitTeam opponentTeam in GameObject.FindObjectsOfType<UnitTeam>())
|
|
{
|
|
GameObject opponent = opponentTeam.gameObject;
|
|
if (opponent == null
|
|
|| opponent == mainTarget
|
|
|| opponent == unit.gameObject
|
|
|| opponent.transform.root == unit.transform.root
|
|
|| !myTeam.IsEnemy(opponent))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float sqrDistance = (opponent.transform.position - mainTarget.transform.position).sqrMagnitude;
|
|
if (sqrDistance <= nearestSqrDistance)
|
|
{
|
|
nearestSqrDistance = sqrDistance;
|
|
nearbyOpponent = opponent;
|
|
}
|
|
}
|
|
|
|
return nearbyOpponent;
|
|
}
|
|
}
|