0
\$\begingroup\$

I wish to allow the user to select multiple units by click and drag as most RTS do. The issue I have is that the tutorials are either in 2D or only use the transform.position such as this one.

Using the transform.position will work for most cases but will not allow the user to select easily as selecting the head or feet of a unit will not work in this instance. It also means I have to do an other script using raycasts for single click selections as it is almost impossible to get the single point of the transform.position with one click.

How can I do a proper rectangle selection for a 3D game? By looking at 3D RTS games I own, I noticed that they use a simplified hitbox (usually a cube or capsule).

\$\endgroup\$
  • \$\begingroup\$ Can you show us what a typical perspective looks like in your game? This can change how we implement features like this. \$\endgroup\$ – DMGregory Jul 28 at 18:16
  • \$\begingroup\$ I currently do not have anything to show but a good example would be Supreme Commander 2 or Age of Empires 3. I would define the perspective as 3D, not isometric, with the possibility to move and rotate the camera \$\endgroup\$ – Matthieu Raynaud de Fitte Jul 28 at 18:21
1
\$\begingroup\$

This is a general overview of how to approach the problem. I haven't included any code, but hopefully it's enough to answer your question.

A good way to approach this may be to use Physics.BoxCastAll(). BoxCast is similar to Raycast but, as the name suggests, uses a box instead of a ray.

First, you will need some type of collider on your units that the BoxCast can detect. If you're just using the collider to detect mouse interaction, you can use a box collider with "Is Trigger" checked. For performance reasons, you should put these colliders on their own layer and use a layer mask with the box cast.

The tricky part would probably be converting the 2D box drawn by the mouse into a world-space 3D box. One way to do this may be to find each corner of the box in screen space, convert the corners to world space, then calculate the center and extents of the box based on these corners. I think that you could just use the camera's rotation for the box's rotation, but am not 100% certain as I haven't tried this before. EDIT: As noted by DMGregory in the comments, you will probably also need to account for the camera frustum, which makes things trickier and may require using multiple box casts, or instead calling Physics.OverlapBox() multiple times for different distances.

If you go with this setup, you can just use a regular raycast for regular single-click selection. That's also a lot simpler than the highly complex selection manager from your link.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Would it be preferable to use OverlapBoxNonAlloc instead, since we just want to check a box-shaped region, not sweep a box through space? I think you may also need to do this in multiple slices, to account for the widening of the frustum as it recedes in depth. \$\endgroup\$ – DMGregory Jul 28 at 19:19
  • \$\begingroup\$ @DMGregory I was thinking that OverlapBox wouldn't be the best choice because the game uses a 3D camera and we don't know how far away from the camera our selectable objects might be. However, I suppose we could set the depth of the box to be very high and it would work out about the same as a BoxCast? \$\endgroup\$ – Kevin Jul 28 at 20:10
  • \$\begingroup\$ @DMGregory I think you're right about the frustum. I'm used to using camera.ScreenPointToRay() and not having to think about the frustum. \$\endgroup\$ – Kevin Jul 28 at 20:17
  • \$\begingroup\$ Thanks for the tips, I will give it a try and accept it as an answer if it works \$\endgroup\$ – Matthieu Raynaud de Fitte Jul 29 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.