Make the rectangle with Rect2(start, end - start).abs() before you test each unit's screen position with has_point(). Dragging up or to the left makes end - start negative. In our Godot 4.7 test the same box selected 9 units when dragged down and right, and 0 when dragged up and left from the opposite corner, with an error for every unit checked: Rect2 size is negative. With abs() both directions selected the same 9.

The code
Put this script on a Control under a CanvasLayer and add your units to the units group. Connect selected to your own code to store the selection and show selection rings. The box draws while you drag. The Control itself ignores the mouse (mouse_filter is Ignore), it only draws.
extends Control
## Drag a box with the left mouse button to select 3D units, RTS style.
## Put this under a CanvasLayer. Units must be Node3Ds in the "units" group.
## "selected" sends the units whose position falls inside the box when you let go.
signal selected(units: Array[Node3D])
@export var group := "units"
@export var color := Color(0.3, 0.8, 1.0)
var _start := Vector2.ZERO
var _end := Vector2.ZERO
var _dragging := false
func _ready() -> void:
set_anchors_preset(PRESET_FULL_RECT)
mouse_filter = MOUSE_FILTER_IGNORE # the box only draws, clicks still reach the game
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
_dragging = true
_start = event.position
_end = event.position
elif _dragging:
_dragging = false
_end = event.position
selected.emit(units_in(box(_start, _end)))
queue_redraw()
elif event is InputEventMouseMotion and _dragging:
_end = event.position
queue_redraw()
func box(a: Vector2, b: Vector2) -> Rect2:
return Rect2(a, b - a).abs() # dragging up or left gives a negative size, and has_point() then finds nothing
func units_in(rect: Rect2) -> Array[Node3D]:
var cam := get_viewport().get_camera_3d()
var out: Array[Node3D] = []
for n in get_tree().get_nodes_in_group(group):
var u := n as Node3D
if u and not cam.is_position_behind(u.global_position) and rect.has_point(cam.unproject_position(u.global_position)):
out.append(u)
return out
func _draw() -> void:
if _dragging:
var r := box(_start, _end)
draw_rect(r, Color(color, 0.2))
draw_rect(r, color, false, 2.0)
What we measured
Twelve units in a 4 by 3 grid, 2 m apart, seen from a camera 12 m up and 8 m back, on a 960 by 540 screen. A box from (445, 222) to (578, 329) covered nine of them.
Rect2(start, end - start)dragged from the top left to the bottom right: 9 units.- The same box dragged from the bottom right to the top left: size (-133, -107), 0 units, and 12 lines in the output, one per unit checked:
ERROR: Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size. - The script's
box(), both directions: 9 units. - A mouse press at the bottom right, a drag and a release at the top left, sent to the script as input events:
selectedsent 9 units.
Two runs gave the same numbers.
Why dragging up or left selects nothing
A Rect2 is a corner plus a size. When you drag up or left, the end is above or left of the start, so the size comes out negative. has_point() needs the point to be at or past the corner and before the corner plus the size, and with a negative size no point can be both. abs() moves the corner to the top left and makes the size positive, so it covers the same area the player dragged over.
What to change
- Moving the selected group without a pile-up: formation spots that arrive together.
group: the group your selectable units are in.- The script tests each unit's origin. If your units' origins are at their feet and the camera is low, test a point at chest height instead.
- A click without dragging makes a tiny box. Pick the unit under the mouse with a ray for a click, and use the box only when the mouse has moved a few pixels.
- Units behind the camera are skipped, for the reason in an icon over a 3D character's head.
Ready-made RTS and siege effects: Medieval Warfare & Siege VFX for Godot 4 has 33 presets in Stylized and Retro PSX looks, including Unit Select rings in blue and red, Move Order and Attack Order markers, a Rally Point, arrow volleys that stick in the ground and a trebuchet.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Medieval Warfare & Siege VFX for Godot 4 has 33 ready-made siege effects in Stylized and Retro PSX looks, including Unit Select rings, Move Order and Attack Order markers, arrow volleys that stick in the ground and a trebuchet.
More how-tos for Medieval Warfare & Siege VFX for Godot 4: Hit the target with an arcing shot at any angle and height · Move a group of units without a pile-up
More how-tos: UI and HUD
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total