← Pixel Dreamer Studio

Fan out any number of projectiles evenly around the aim direction

Go from one edge of the fan to the other in count - 1 steps, and put a single projectile straight ahead.

A spread shot needs angles from one edge of the fan to the other. In our Godot 4.7 test of a 30 degree fan, stepping by spread / (count - 1) worked from 2 projectiles up, but with 1 it divided by zero and the direction came out as nan, not a number, so the shot went nowhere. Stepping by i * spread / count never reached the far edge and pulled the middle of the fan 2.5 to 7.5 degrees to one side. The script lerps from edge to edge and puts one projectile straight ahead: centered for every count.

The code

Call spread_angles(count, 30.0) and rotate your aim direction by each angle, for example aim.rotated(Vector3.UP, a), then fire one projectile along each.

extends Node
## Angles for a fan of projectiles (spread shot, multi-cast) centered on the aim
## direction, for any count, including 1.


static func spread_angles(count: int, spread_degrees: float) -> PackedFloat32Array:
	var out := PackedFloat32Array()
	for i in count:
		var t := 0.5 if count == 1 else float(i) / (count - 1)  # 0 to 1 across the fan
		out.append(deg_to_rad(lerpf(-spread_degrees / 2.0, spread_degrees / 2.0, t)))
	return out

What we measured

A 30 degree fan with 1 to 6 projectiles, each angle turned into a direction with rotated(). Two runs with the same numbers.

Why it happens

A fan of n projectiles has n - 1 gaps, not n. Dividing by n leaves one gap's worth of fan unused at the far edge. Dividing by n - 1 is right, until n is 1 and there are no gaps at all: 30 divided by 0 is infinity, and 0 times infinity is not a number. A single projectile has to be its own case.

What to change

Aim the fan at the mouse first: aim a spell at the mouse in 3D. Fast projectiles that skip enemies: a raycast every frame.

Projectiles to fire: Projectile & Cast VFX for Godot 4 has 41 spell projectiles, each with its cast, flight and hit in one scene.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Projectile & Cast VFX for Godot 4 has 41 spell projectiles, each with its cast, flight and hit in one scene.

More how-tos for Projectile & Cast VFX for Godot 4: Spawn a hit effect facing the surface

More how-tos: Projectiles and aiming