示例#1
0
    private RESULT Select_center()
    {
        List <Unit> units = _unitGroup.GetOpponents(_unit.team);

        int     unitCount  = 0;
        Vector3 unitCenter = new Vector3();

        int n = units.Count;

        for (int i = 0; i < n; ++i)
        {
            Unit enemyUnit = units[i];
            if (enemyUnit.isDead)
            {
                continue;
            }

            ++unitCount;
            unitCenter += enemyUnit.transform.position;
        }

        if (unitCount > 0)
        {
            unitCenter /= unitCount;

            Unit  target  = null;
            float minDist = int.MaxValue;

            for (int i = 0; i < n; ++i)
            {
                Unit enemyUnit = units[i];
                if (enemyUnit.isDead)
                {
                    continue;
                }

                Vector3 offset = enemyUnit.transform.position - unitCenter;
                float   dist   = offset.magnitude;
                if (dist < minDist)
                {
                    target  = enemyUnit;
                    minDist = dist;
                }
            }

            if (target != null)
            {
                RESULT r = new RESULT();
                r.target = target;
                r.offset = minDist;
                return(r);
            }
            else
            {
                return(null);
            }
        }
        else
        {
            return(null);
        }
    }