Selecting objects inside selection box

edit//

Ok so I have mapped the box from the screen to the terrain and set up bounds to test weather an object is inside it. (I know this test only works on the first quadrant, im just trying to get it working right now)

However no luck, can figure out where my logic went wrong

if(Input.GetMouseButtonUp(0))
	{
		Vector.DestroyLine(ref selectionLine);//just removes the box I just drew
		
		var ray1 = Camera.main.ScreenPointToRay(originalMousePos); //first corner of the selection box
		var ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);//second corner
	
		RaycastHit hit1;
		RaycastHit hit2;		
		Physics.Raycast(ray1, out hit1, Mathf.Infinity, ~(1 << 10)); //cast a ray from the 1st position
		Physics.Raycast(ray2, out hit2, Mathf.Infinity, ~(1 << 10)); //cast a ray from the 2nd position
		
		var objectPosition = this.gameObject.transform.position;

		if((objectPosition.x > hit1.point.x && objectPosition.x < hit2.point.x) && (objectPosition.z > hit1.point.z && objectPosition.z < hit2.point.z))
		{
			ObjectManager.SelectedObjects.Add(this.gameObject); //test if the object is inside the bounds			
		}	
				
	}	

End edit//

I already have a successful selection box display working. However im not sure how to detect the objects inside the selection box in order to select them.

Useful information I have is

originalMousePosition
CurrentMousePosition

those two variables are used to calculate the box.
I originally thought to check the objects coordinates against the bouding box of the selection Box. But the selection box is a 2D overlay and its in Screen (Viewpoint) coordinates. So Im not to sure where to go from here.

1 Like

3 Answers

3

Can I assume this is something like marquee selection in an RTS?

What I do is I run Camera.WorldToScreenPoint on all the “Units” and “Buildings” in my game. Objects with a negative z value are behind the camera and I can ignore them. From there I can just do a simple check to see if the “Unit’s” position in screen coordinates is between originalMousePosition and CurrentMousePosition.

Originally I tried to use Camera.ScreenToWorldPoint but when you create a bounding box from those points it is, by it’s nature, axis aligned. It’s hard to explain but the result is that the box created isn’t relative to your camera. The easiest way to see what I mean is to draw things by hand or have Unity3D actually drawing the bounding box you think should appear.

Exactly. I found it a lot easier to think in terms of what my camera see's than trying think of some imaginary 3D box.

–

I think you are rather close the the goal.

I did a quick search for ScreenTo functions: http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=screento

Camera.ScreenToViewportPoint

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToViewportPoint.html

Camera.ViewportToScreenPoint
http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportToScreenPoint.html

I made this same thing just by setting in Inspector “Layer: Ignore Raycast” for selection box.
In most cases you don’t need to hit in selection box but in object.