0
\$\begingroup\$

I have an issue with a script the community helped with previously. If an enemy enters the player's ring, it is assigned to the player's list of enemies to combat. However, if a group of enemies gets inside this ring I need to sort them by distance from player, so that the first one in the list will be the closest enemy from player, and so on. If an enemy dies, the next closest enemy should be promoted. Will my GameObject list allow me to do this? Or I need to make new Vector3 list? I'm using System.Linq.

        public List<GameObject> enemiesInRange = new List<GameObject>();
        public int hox;

    // Update is called once per frame
    void Update()
    {

        hox = enemiesInRange.Count;
        if (hox == 3)
            print("Frist enemy near you is " + enemiesInRange[0] + ", " + enemiesInRange[1] + ", " + enemiesInRange[2]);
        //   enemiesInRange[0].GetComponent<enemypath>().enemey_checker = true;
        //   enemiesInRange[1].GetComponent<enemypath>().enemey_checker = true;
        //   enemiesInRange[2].GetComponent<enemypath>().enemey_checker = true;

    }


    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("enemy") && hox < 3)
            enemiesInRange.Add(other.attachedRigidbody.gameObject);
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("enemy"))
            enemiesInRange.Remove(other.attachedRigidbody.gameObject);
    }

I really hope to solve this issue and finish from this point.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

When in doubt, I recommend consulting the documentation for the types you're using. Even using Intellisense to browse the members of List<T> would let you discover List.Sort().

This lets you sort a list using any Comparison method you choose, like for instance this one:

int CompareDistanceToMe(GameObject a, GameObject b) {
    float squaredRangeA = (a.transform.position - transform.position).sqrMagnitude;
    float squaredRangeB = (b.transform.position - transform.position).sqrMagnitude;
    return squaredRangeA.CompareTo(squaredRangeB);
}

Then you can simply call:

enemiesInRange.Sort(CompareDistanceToMe);

This is a quick & dirty solution that does strictly more vector calculations than necessary, but for a handful of enemies that won't be a problem. If you want to run this on hundreds of enemies then you'll want to pre-compute your distances first, then find the ranked/sorted order of that collection.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ By the way: The Reason why DMGregory is using sqrMagnitude here instead of magnitude is because the square of the magnitude of a vector is faster to compute than the actual magnitude. If you only care about which one is closer, then comparing the squares of the distances instead of the actual distances does the job just as well and saves you some CPU cycles. \$\endgroup\$
    – Philipp
    Commented Jan 5, 2019 at 17:34
  • \$\begingroup\$ Thank you guys for all what you do here. I spend my day reading about the OrderBy & ToList, reading some saying crazy things, or make new vector3 and merge both lists...headache. Really good to have people here give you the solution and explain why he did this and where is your mistake to learn. \$\endgroup\$
    – Omer
    Commented Jan 5, 2019 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .