Finding minimum/maximum value of a hashtable

Making a navagent guider that makes a host follow the closest location and change when it finds an even closer one.

        GameObject[] targets;
        targets = GameObject.FindGameObjectsWithTag("GreenTeam");
                Hashtable distances;
                distances = new Hashtable();
                foreach (GameObject gameobject in targets)
                {
                    distances.Add(gameobject,Vector3.Distance(transform.position,gameobject.transform.position));
                }

What comes to mind, when I think of this is:

  1. use a list that you sort
  2. use sqrMagnitude instead of distance
  3. use a small class (for the list), which has a game object and a float (that float - sqrMagnitude - is what you use to sort)
  4. possibly include some threshold, so the decision doesn’t change if you’re already following but something else is momentarily closer
  5. don’t use Find, but maintain a valid list.
  6. some update frequency less than once per frame.

Hope that helps :slight_smile: