Object with the closest vector3 position to the desired?

Hello, i want to make an object move to the closest available position that matches a given criteria. In this case something like

             if(Input.GetAxisRaw("Horizontal") != 0)
                {
                    /*move to the object with the closest horizontal position in the pressed direction.
                      something like getting an array of gameobjects, and then deciding which one is closest 
                      to this object's position */
                }

I remember doing this once before, something like getting an array and checking which was closest to gameobject.transform.position.x + input.getaxisraw(“Horizontal”), but i cannot remember what i did to make that calculation.

You just have to iterate over all of the objects you’re interested in and calculate the distance. Find the one with the smallest distance. A little more efficient if you use Vector3.SqrDistance instead of Vector3.Distance. Since you’re only interested in one “side”, you should also throw out any objects that are not on that side by comparing the X component of your object with each object.

what should i do in order to calculate the distance?

Vector3.Distance(<position of one of the objects>, <position of the other object>)

Ah, i didnt know there was a function for vector distance. I remember using some sort of calculation method

That would probably be something like this:

//Where "position1" and "position2" are Vector3 or Vector2 values.
float distance = (position1 - position2).sqrMagnitude;

in the same vein of questions, how would i get the object farthest to the left in the scene using vector3.distance

If you just need to know which object is the farthest to the left in a scene, you don’t actually need to use any distance calculations; you can simply just find the object with the lowest X-position value.

Given a collection of GameObjects, loop through and compare the X-position of each, caching the object with the lowest value and returning it at the end of the loop:

GameObject GetLeftMostObjectInScene(GameObject[] sceneObjects) {
   GameObject leftMostObject = null;
   float lowestXPosition = float.MaxValue;

   for(int i = 0; i < sceneObjects.Length; i++) {
      float xPostion = sceneObjects[i].transform.position.x;

      if(xPostion < lowestXPosition) {
         lowestXPosition = xPostion;
         leftMostObject = sceneObjects[i];
      }
   }

   return leftMostObject;
}

How you get the collection of GameObjects is up to you.

thank you. is there a quick way to reverse this to get farthest to the right as well? i tried to make it work both ways, but it didnt quite work out.

    GameObject GetFarthestObject(int Direction, GameObject[] Objects)
    {
        GameObject FurthestObject = null;
        float FurthestPosition = (100f * (Direction * -1));

        for (int i = 0; i < Objects.Length; i++)
        {
            float xPostion = Objects[i].transform.position.x;
            if(FurthestPosition == 100f)
            {
                if (xPostion < FurthestPosition)
                {
                    FurthestPosition = xPostion;
                    FurthestObject = Objects[i];
                    Debug.Log("Furthest: " + FurthestPosition);
                }
            }
            else if(FurthestPosition == -100f)
            {
                if (xPostion > FurthestPosition)
                {
                    FurthestPosition = xPostion;
                    FurthestObject = Objects[i];
                    Debug.Log("Furthest: " + FurthestPosition);
                }
            }
        }

        return FurthestObject;
    }

i used 100f because that makes it so that no matter how far to the left/right the object is, this method should be able to find it. as for why it’s multiplied by (Direction * -1), its just to make it easier to read IMO, as its called with -1 for left and positive 1 for right

To make it work both ways, it may be easier to just write a struct that contains the farthest objects in both directions that will be returned by the method, and the method would just calculate both directions at the same time.
Something like this:

public readonly struct FarthestObjects {
   public GameObject FarthestObjectLeft { get; }
   public GameObject FarthestObjectRight { get; }

   public FarthestObjects(GameObject farthestLeft, GameObject farthestRight) {
      FarthestObjectLeft = farthestLeft;
      FarthestObjectRight = farthestRight;
   }
}
FarthestObjects GetFarthestObjectsInScene(GameObject[] sceneObjects) {
   GameObject farthestLeft, farthestRight = null;

   float lowestX = float.MaxValue;
   float highestX = float.MinValue;

   for(int i = 0; i < sceneObjects.Length; i++) {
      float xPostion = sceneObjects[i].transform.position.x;

      if(xPostion < lowestX) {
         lowestX = xPostion;
         farthestLeft = sceneObjects[i];
      }

      if(xPostion > highestX) {
         highestX = xPostion;
         farthestRight = sceneObjects[i];
      }
   }

   return new FarthestObjects(farthestLeft, farthestRight);
}

This could also be extended to include the farthest objects on the other axes, if needed:

public readonly struct FarthestObjects {
   public GameObject FarthestObjectLeft { get; }
   public GameObject FarthestObjectRight { get; }

   public GameObject FarthestObjectUp { get; }
   public GameObject FarthestObjectDown { get; }

   public FarthestObjects(GameObject farthestLeft, GameObject farthestRight, GameObject farthestUp, GameObject farthestDown) {
      FarthestObjectLeft = farthestLeft;
      FarthestObjectRight = farthestRight;
      FarthestObjectUp = farthestUp;
      FarthestObjectDown = farthestDown;
   }
}
FarthestObjects GetFarthestObjectsInScene(GameObject[] sceneObjects) {
   GameObject farthestLeft, farthestRight, farthestUp, farthestDown = null;

   float lowestX, lowestY = float.MaxValue;
   float highestX, highestY = float.MinValue;

   for(int i = 0; i < sceneObjects.Length; i++) {
      Vector3 position = sceneObjects[i].transform.position;

      if(position.x < lowestX) {
         lowestX = position.x;
         farthestLeft = sceneObjects[i];
      }
      if(position.x > highestX) {
         highestX = position.x;
         farthestRight = sceneObjects[i];
      }

      if(position.y < lowestY) {
         lowestY = position.y;
         farthestDown = sceneObjects[i];
      }
      if(position.y > highestY) {
         highestY = position.y;
         farthestUp = sceneObjects[i];
      }
   }

   return new FarthestObjects(farthestLeft, farthestRight, farthestUp, farthestDown);
}

There’s probably a way to optimize this a bit more, but I’m just free-handing it.