selecting objects inside a box part2

I want to select all the objects inside of a RTS style selection box.

Since I have originalmousePosition and currentMousePosition, Im looping though all the units and converting there coordinates to screen coordinates. From there Im testing weather the screen coordinates are located inside the box I drew. If the units are inside the box, it selects them.

I believe my code is “generally” right, but I dont think i have my bounds set up to represent the area inside the box. Any help please?

public static void selectMultipleObjects(Vector2 originalPos, Vector2 currentPos)
{
	foreach(GameObject go in movableObjectsList) //represents all the movable units
	{
		var screenCoordinates = Camera.main.WorldToScreenPoint(go.transform.position);//convert the current object position to screen coordinates
		
		//Find all the objects inside the box
		if((screenCoordinates.x < originalPos.x && screenCoordinates.x > currentPos.x) && (screenCoordinates.y > originalPos.y && screenCoordinates.y < currentPos.y))
		{
			if(!DoesObjectExistInList(movableObjectsList, go))
			{
				SelectedObjects.Add(go);
			}
		}
	}
}

Unity has support for boxes with Rect. Assuming originalPos and currentPos are the corners of your selection box, you can build your box with:

Rect selectionBox = new Rect(Mathf.Min(originalPos.x, currentPos.x)), Mathf.Min(originalPos.y, currentPos.y),
                             Mathf.Abs(originalPos.x - currentPos.x),
                             Mathf.Abs(originalPos.y - currentPos.y));

Then you can replace your if statement with:

if(selectionBox.Contains(screenCoordinates))

And you should be good to go.

I think y should be inverted like this:
Rect selectionBox = new Rect(Mathf.Min(originalPos.x, currentPos.x)),
Screen.height - Mathf.Max(originalPos.y, currentPos.y),
Mathf.Abs(originalPos.x - currentPos.x),
Mathf.Abs(originalPos.y - currentPos.y));