Search Unity

Finding minimum/maximum value of a hashtable

Discussion in 'Scripting' started by Deleted User, Apr 21, 2018.

  1. Deleted User

    Deleted User

    Guest

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

    Code (CSharp):
    1.         GameObject[] targets;
    2.         targets = GameObject.FindGameObjectsWithTag("GreenTeam");
    3.                 Hashtable distances;
    4.                 distances = new Hashtable();
    5.                 foreach (GameObject gameobject in targets)
    6.                 {
    7.                     distances.Add(gameobject,Vector3.Distance(transform.position,gameobject.transform.position));
    8.                 }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)