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.
lerp_angle()on the needle: 0 to 50 and 0 to 60 stayed on the dial. 0 to 75: 6 frames in the gap, 38.1 degrees deep. 0 to 100 and 100 to 0: in the gap on all 120 frames, 43.8 degrees deep.- The script: 0 frames in the gap for all five jumps, and the same end angles.
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
- A dial with another sweep: set
min_angleandmax_angle. - A ring gauge drawn with draw_arc() can use the same smoothed value for its end angle.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total