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.
spread / (count - 1): 1 projectile gave an angle of nan and a direction that is not finite. 2 to 6 were right, from -15 to 15.i * spread / count: always started at -15 but stopped short of 15. The middle of the fan sat at -15, -7.5, -5, -3.8, -3 and -2.5 degrees for 1 to 6.- The script: 0 for 1 projectile, -15 to 15 for the rest, the middle at 0 every time.
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
- A fan that keeps the same gap instead of the same width: use count - 1 times the gap as the spread.
- For 2D, use the angles with Vector2.rotated().
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
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- A fire rate that holds at any frame rate
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Recoil that settles back to where the player aims