- Home /
Accurate RTS Selection
OK so I've been using the following code to do my RTS style selection:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out info);
If the camera is down (90, 0, 0) the accuracy of my selection box is 100%. However, the more I rotate the camera, the more depth inaccuracies occur. For instance, the way my selection box works is:
1) At the start of drag record an initial point in space via the above code.
2) At the end of drag record an ending point in the space via the above code.
3) Iterate through the game objects to see which ones reside within the rectangle.
Note: The rectangle is definitely working properly; it is in world space and not screen space.
So, that's what happens. It seems to me that the depth of the clicks are not right.
Answer by Eric5h5 · Nov 13, 2010 at 11:18 PM
Instead of trying to use a world-space selection box like that, see if the objects' coordinates when converted to screen space are within the screen-space rectangle instead.
Edit: indeed this method does only test for the object's pivot, but I think you actually want that; certainly it's typical in games (the bottom two here aren't selected in case it's not obvious):
it has it's own drawbakcs too. because sometimes pivots of objects are out of the rectangle but a big part of them is inside however it's a good solution.
@Ashkan: true, but I'm not sure that's actually a drawback, or at least it seems natural, maybe because that's what other games do. See my edit.