← Pixel Dreamer Studio

Drag a box to select 3D units, in any direction

Build the rectangle with Rect2.abs() before has_point().

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.

Twelve blue units seen from above with the same blue selection box over nine of them, twice. On the right the nine inside the box have green rings
Godot 4.7. The same box, dragged from the bottom right to the top left. Left: Rect2(start, end - start).has_point(). Right: the script below.

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.

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

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