GDScript ships the math functions games need as global functions. These are the ones you reach for most:
100
2.5
2
7.0
3.14
Most have three forms: a general one (clamp), one for ints (clampi) and one for floats (clampf). The typed ones let the editor check your code, so prefer them when you know the type.
clamp: Keep a Value in a Range
100
0
(100.0, 0.0)
Clamping in one place, such as a setter, keeps health, ammo and volume from ever leaving their range.
lerp, inverse_lerp and remap
lerp(a, b, t) goes from a (at t = 0) to b (at t = 1). inverse_lerp() does the opposite and remap() converts a value from one range to another:
150.0
0.25
50.0
remap() turns a distance into a volume, a speed into an animation rate, or health into a bar width.
Smooth Movement with lerp
Calling lerp() every frame toward a target gives a smooth slow-down, often used for cameras:
func _process(delta):
# moves 10% of the remaining way per frame at 60 fps, slowing down near the target
position = position.lerp(target.position, 1.0 - exp(-6.0 * delta))
The 1.0 - exp(-speed * delta) weight makes the smoothing the same at any frame rate. A fixed weight like 0.1 would move faster on a 144 Hz screen than on a 60 Hz one.
move_toward: Constant Speed
move_toward(from, to, delta) changes a value by at most delta and stops exactly at the target:
3.0
6.0
9.0
10.0
This is the standard way to accelerate a character to a top speed: velocity.x = move_toward(velocity.x, max_speed, acceleration * delta).
wrap: Loop Around a Range
Quit
Play
10.0
-1 4
The max of wrapi() is not included. For negative numbers, % keeps the sign (-1 % 5 is -1), while posmod() always returns a value from 0 up.
Rounding
3.0 -3.0
2.0 3.0
-2 -3
15 0.7
round(),floor()andceil()return floats.roundi(),floori()andceili()return ints.int()drops the decimals, which rounds toward zero:int(-2.7)is-2, butfloori(-2.7)is-3.snapped()rounds to the nearest multiple, which suits grid snapping.
Angles Are in Radians
Godot's trigonometry and rotation use radians. Convert with deg_to_rad() and rad_to_deg():
3.14159265358979
90.0
1.0 1.0
20.0
angle_difference() gives the shortest turn between two angles, so turning from 350 to 10 degrees is 20 degrees, not 340. Nodes also have rotation_degrees when you prefer degrees.
More Helpers
| Function | Gives |
|---|---|
abs(x), sign(x) | the size of x, and -1, 0 or 1 |
min(a, b, ...), max(a, b, ...) | the smallest or largest |
sqrt(x), pow(x, y), x ** y | root and powers |
is_equal_approx(a, b) | whether two floats are equal within rounding |
smoothstep(from, to, x) | an eased 0 to 1 curve |
lerp_angle(a, b, t) | lerp that takes the short way around |
fposmod(x, y) | posmod for floats |
Frequently Asked Questions
How do I clamp a value in Godot?
clamp(value, min, max) keeps a value inside a range: hp = clamp(hp, 0, 100). Use the typed versions clampi() for ints and clampf() for floats, which the editor can check. Vectors have their own clamp() method.
What does lerp do in Godot?
lerp(from, to, weight) returns the value a fraction of the way from one number to another: lerp(0.0, 10.0, 0.25) is 2.5. Called every frame with a small weight it makes smooth follow and fade effects.
What is the difference between move_toward and lerp?
move_toward(from, to, delta) moves by a fixed amount and stops exactly at the target, which suits constant speed. lerp() moves by a fraction of the remaining distance, so it starts fast and slows down near the target.
How do I wrap a value around in Godot?
wrapi(value, min, max) for ints and wrapf() for floats bring a value back into the range from the other side: wrapi(5, 0, 5) is 0 and wrapi(-1, 0, 5) is 4. The max is not included. This is how a menu cursor loops from the last item to the first.