I had queried this problem many times in the past but never needed to try and solve it until this week. The problem with selecting object/s via given screen rect coordinates within Unity is that if your camera’s view is not orthographic…which in most cases it isn’t, your rectangle, projected collision detection or raycast will need to take the form of a trapezoid shape i.e. matching the camera frustrum, fanning out from the screen rectangle.

You could create a mesh of this shape and try some sort of collision detection, but perhaps if you are using many mesh colliders this may not be an easy task and carry too much overhead.

Here are two pretty useful functions that will solve this task, both functions take two screen coordinates in the form of Vector3’s (as in Input.mousePosition), and an array of GameObjects to test against however they return different results:-

The first function returns an array of GameObjects where the center of the GO’s renderer bounds is within the camera projected rectangle/trapezoid – This method is probably only useful on small non-enclosed meshes however it does allow the user not to have to completely surround objects with the marquee.

The second function returns an array of GameObjects where the meshes local OBB points are within the camera projected rectangle – This method provides much more accuracy with selection however an objects bounds must be completely encompassed by the given screen rect.

One further note is that this example utilises camera.main to refer to the main camera in the scene, so if you want to use this on another camera/view or to be a more flexible function you will need to amend the function to perhaps take the required camera as an argument.

Hope this helps somebody.

2802 Total Views 1 Views Today