For a fixed launch angle a, use speed = sqrt(g d² / (2 cos²a (d tan a - h))), where d is the flat distance and h how much higher the target is. The shortcut sqrt(gravity * distance) is only right on flat ground at 45 degrees. In our Godot 4.7 test it missed a target 5 m higher and 40 m away by 6.14 m. The script missed by 0.29 m.
The code
Call launch_velocity(muzzle_position, target_position) and give the result to your projectile as its starting velocity, then apply gravity to it every frame. Use the same gravity value your projectile uses. A result of Vector3.ZERO means the target is too high to reach at this angle: raise the angle or pick another target.
extends Node
## Launch velocity to hit a target at a fixed angle: catapult, trebuchet, mortar, grenade.
## Works when the target is higher or lower than the launcher. Returns Vector3.ZERO when
## the target cannot be reached at this angle (too high for it).
@export var angle_degrees := 45.0
@export var gravity := 9.8
func launch_velocity(from: Vector3, to: Vector3) -> Vector3:
var flat := Vector3(to.x - from.x, 0.0, to.z - from.z)
var d := flat.length()
var h := to.y - from.y # positive = target is higher
var a := deg_to_rad(angle_degrees)
var denom := 2.0 * cos(a) * cos(a) * (d * tan(a) - h)
if d < 0.001 or denom <= 0.0:
return Vector3.ZERO
var speed := sqrt(gravity * d * d / denom)
return flat.normalized() * speed * cos(a) + Vector3.UP * speed * sin(a)
What we measured
We flew each shot at 60 steps per second, the usual way: subtract gravity from the velocity, then move by the velocity. We measured how far from the target it was when it came down to the target's height. Gravity 9.8, two runs with the same numbers.
- 45 degrees, flat ground, 20 m and 60 m: both ways missed by 0.17 m and 0.29 m.
- 45 degrees, target 5 m higher at 20 m: the shortcut never came back down to the target's height on the way in. The script missed by 0.29 m.
- 45 degrees, target 5 m lower at 20 m: shortcut 4.00 m, script 0.12 m. Target 5 m higher at 40 m: shortcut 6.14 m, script 0.29 m.
- 60 degrees: the shortcut missed by 2.80 m on flat ground at 20 m, 6.50 m with the target 5 m higher, 8.72 m at 40 m and 5 m higher, and 8.24 m on flat ground at 60 m, but only 0.26 m with the target 5 m lower at 20 m, where the two errors happen to cancel. The script missed by 0.10 to 0.22 m.
- 30 degrees at a target 10 m away and 8 m up: the script returned Vector3.ZERO, because no speed reaches it at that angle.
Why it happens
sqrt(g d) comes from the range formula for flat ground, and at 45 degrees the angle terms cancel out. Change the angle or put the target on a wall or down a cliff and it no longer applies. The 0.1 to 0.3 m the script still misses by comes from moving in 1/60 second steps, while the formula assumes smooth motion. Splash damage or a hit radius of half a meter covers it.
What to change
angle_degrees: the launch angle. Steeper arcs look heavier and clear walls.gravity: must match the gravity your projectile falls with.
Draw the path before you fire: predict a trajectory line.
The launch and the landing: Medieval Warfare & Siege VFX for Godot 4 has Trebuchet Launch, Boulder Impact, Wall Hit, Wall Breach and Fire Pot Impact.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Medieval Warfare & Siege VFX for Godot 4 has Trebuchet Launch, Boulder Impact, Wall Hit, Wall Breach, Fire Pot Impact and Arrow Volleys that arc and stick in the ground.
More how-tos for Medieval Warfare & Siege VFX for Godot 4: Drag a box to select 3D units, in any direction · Move a group of units without a pile-up
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
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims