← Pixel Dreamer Studio

A tower's attack range ring

One number for the ring the player sees and the area that finds enemies.

Keep the range in one variable and set both the ring and the detection shape from it, so the circle the player sees is the range the tower really uses. Draw the ring as a flat band of triangles just above the floor. Give the Area3D a CylinderShape3D: a sphere missed a flying enemy that stood inside the ring in our test.

A gray tower with a blue ring around it on a dark floor, and a see-through tower on the right with a red ring
The script below in Godot 4.7: a placed tower in blue, and a tower held over a spot where it cannot go in red.

The code

Add a Node3D as a child of the tower, at its foot, and give it this script. Connect enemy_entered and enemy_exited, or call enemies_in_range() when the tower is ready to shoot.

extends Node3D
## A tower's attack range: a ring on the ground and an Area3D, both sized from attack_range.
## Add it as a child of the tower, at the foot. Set attack_range again when the tower upgrades.

signal enemy_entered(body)
signal enemy_exited(body)

@export var attack_range := 5.0:  # meters
	set(v):
		attack_range = v
		if is_node_ready():
			_apply()
@export var ring_width := 0.08  # meters
@export var ok_color := Color(0.3, 0.8, 1.0)
@export var bad_color := Color(1.0, 0.25, 0.2)
@export var valid := true:  # false while the player holds the tower over a spot where it cannot go
	set(v):
		valid = v
		_mat.albedo_color = ok_color if valid else bad_color

var area := Area3D.new()
var _shape := CylinderShape3D.new()  # a cylinder, not a sphere: height does not change the range
var _ring := MeshInstance3D.new()
var _mat := StandardMaterial3D.new()


func _ready() -> void:
	var cs := CollisionShape3D.new()
	cs.shape = _shape
	area.add_child(cs)
	area.monitorable = false
	add_child(area)
	area.body_entered.connect(func(b): enemy_entered.emit(b))
	area.body_exited.connect(func(b): enemy_exited.emit(b))
	_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	_mat.cull_mode = BaseMaterial3D.CULL_DISABLED
	_mat.albedo_color = ok_color if valid else bad_color
	_ring.material_override = _mat
	_ring.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	add_child(_ring)
	_apply()


func show_ring(on: bool) -> void:
	_ring.visible = on


func enemies_in_range() -> Array:
	return area.get_overlapping_bodies()


func _apply() -> void:
	_shape.radius = attack_range
	_shape.height = 40.0  # tall enough for flying enemies
	var outer := attack_range
	var inner := maxf(attack_range - ring_width, 0.0)
	var steps := 96
	var v := PackedVector3Array()
	for i in steps:
		var d0 := Vector3(cos(TAU * i / steps), 0, sin(TAU * i / steps))
		var d1 := Vector3(cos(TAU * (i + 1) / steps), 0, sin(TAU * (i + 1) / steps))
		for p in [d0 * inner, d0 * outer, d1 * outer, d0 * inner, d1 * outer, d1 * inner]:
			v.append(p + Vector3(0, 0.02, 0))  # just above the floor, so it does not flicker
	var arr := []
	arr.resize(Mesh.ARRAY_MAX)
	arr[Mesh.ARRAY_VERTEX] = v
	var m := ArrayMesh.new()
	m.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
	_ring.mesh = m

From a camera straight above, the outer edge of the ring was 5.01 meters from the tower with the range at 5, and 6.99 meters after we set it to 7. One pixel was 0.04 meters at that height.

An enemy counts when its edge reaches the ring

The Area3D checks the collision shape of the enemy, not its center. An enemy with a 0.3 meter sphere counted at 5.2 and 5.25 meters from the tower, and not at 5.35. So the tower starts shooting about one enemy radius before the center crosses the ring.

Why a cylinder, not a sphere

A flying enemy 3 meters up and 4.5 meters away looks inside the ring from above. With a CylinderShape3D, the Area3D found it. With a SphereShape3D of the same 5 meter radius, it did not: from the foot of the tower that enemy is about 5.4 meters away in 3D.

Upgrades and bad spots

Set attack_range when the tower levels up. The setter rebuilds the ring and resizes the shape: going from 5 to 7, an enemy at 6.5 meters counted. Set valid to false while the player holds the tower over a spot where it cannot go. The ring then read red 1.0, green 0.25, blue 0.2 on the screen. show_ring(false) hides it while the area keeps working: 0 lit points on the screen.

What to change

Why the ring sits 0.02 meters above the floor: a glowing circle on the ground without flicker. A circle that follows the mouse instead, for an area spell: aim a spell at the mouse on a 3D floor.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Tower Defense VFX for Godot 4 has animated range rings in normal, selected and invalid versions. They are 4 meters in radius at scale 1, so set the scale to your range divided by 4.

More how-tos: Auras, rings, beams and weather