Making an RTS-style "selection box" for selecting objects in a 3D environment

Godot Version



Hello everybody. To summarize my question, how could I code a function that takes in two positions in a 3D environment and creates a box shape that stretches from one position to the other?

I’m making the basic controls for a real-time strategy game in Godot. The game is 3D, and I want to make the feature where you can click and drag your mouse across the screen to create a box, and any units within that box will be selected when you release the mouse button.

I know how to convert the coordinates of the 2D mouse pointer to a position in the 3D game world. My question is, using those positions, how do I code a box shape so that one corner of the box is on the area where the user first clicked and the opposite corner is wherever the mouse is currently pointing to in the 3D environment?

Thank you for your time.

After some messing around, I figured out how to do it. For anyone who is curious, here’s a brief summary.

dist = the absolute value of the difference between the first and second position vectors

I set the origin of the box to the first position, then subtracted dist / 2, which centers the box between the two positions.

I set the size of the box shape to dist (and added an arbitrary value for the y axis so that the box wouldn’t be completely flat).

I also had to use some if statements to swap around the x positions of the position vectors so that the box would scale properly regardless of whether you’re dragging from left-to-right or right-to-left, etc. It’s rather hard to explain, but if anyone wants an in-depth explanation please let me know.

This method for making a selection box works ok but not as well as I had hoped, so if anyone has their own way of doing this I would love to hear it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.