← Pixel Dreamer Studio

Move a speedometer or gauge needle smoothly without cutting through the bottom

Smooth the number the gauge shows, then turn it into an angle with lerp.

lerp_angle() always takes the shorter way around the circle, which is right for a turret and wrong for a gauge. On a dial from -135 to 135 degrees with the gap at the bottom, jumps from 0 to 50 or 60 were fine in our Godot 4.7 test, but 0 to 75 swung the needle 38.1 degrees into the gap, and 0 to 100, or back, went right through it, up to 43.8 degrees past the end stop. Smoothing the value and mapping it to the angle never left the dial.

The code

Put the script on any node, drag your needle Sprite2D into needle (pivot at the center of the dial, art pointing up), and set target whenever the speed, rpm or heat changes.

extends Node
## Gauge needle (speed, rpm, heat) that sweeps from min_angle to max_angle and never
## cuts through the gap at the bottom: smooth the value, then turn it into an angle.

@export var needle: Node2D
@export var max_value := 100.0
@export var min_angle := -135.0  # degrees, 0 is straight up
@export var max_angle := 135.0
@export var smoothing := 8.0

var target := 0.0
var _shown := 0.0


func _process(delta: float) -> void:
	_shown = lerpf(_shown, clampf(target, 0.0, max_value), 1.0 - exp(-smoothing * delta))
	needle.rotation_degrees = lerpf(min_angle, max_angle, _shown / max_value)

What we measured

A dial from -135 to 135 degrees for values 0 to 100, smoothing 8, at 60 fps for 2 s. Two runs with the same numbers.

Why it happens

From 0 to 100 is 270 degrees the long way, over the top of the dial, and 90 degrees the short way, through the bottom. lerp_angle() picks the 90. Any jump wider than 180 degrees does the same. Smoothing the value keeps the needle on the path the dial really has.

What to change

When the short way is the right way: turret turns the long way around. Another HUD that turns: radar blips that turn with the player.

Dials, rings and gauges to tint: 124 Sci-Fi HUD Elements has 124 sci-fi HUD elements, white on transparent PNG: rings, reticles, radar, gauges, graphs and frames, to tint any color.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: 124 Sci-Fi HUD Elements has 124 sci-fi HUD elements, white on transparent PNG: rings, reticles, radar, gauges, graphs and frames, to tint any color.

More how-tos: UI and HUD