示例#1
0
    public TargetPoint GetClosestTarget(Transform p_enemy)
    {
        TargetPoint closest = null;

        float minDist = float.MaxValue;

        foreach (var item in targetPoints)
        {
            float thisDistance = Vector3.Distance(item.targetPoint.position, p_enemy.position);

            if (item.enemy == p_enemy)
            {
                return(item);
            }

            if (thisDistance < minDist && item.enemy == null)
            {
                minDist = thisDistance;
                closest = item;
            }
        }

        if (closest != null)
        {
            closest.AssignEnemy(p_enemy);
        }

        return(closest);
    }