In Godot 4.7, set pivot_offset_ratio to (0.5, 0.5). Without it, set control.pivot_offset = control.size / 2, and set it again on every resized signal. In our Godot 4.7 test a 200 by 200 ColorRect with the default pivot of (0, 0) moved its center 200 px when turned 90 degrees and 70.7 px when scaled 1.5 times. With the pivot set to the middle once, it turned in place, until it was resized to 300 by 300: then its center was 100 px off again. Updating the pivot on resize, or using pivot_offset_ratio, kept it at 0.
The code
Save it as spin_pivot.gd. keep_centered() puts the pivot in the middle now and after every resize; calling it twice does not connect twice. center_drift() tells you how many pixels rotation and scale have moved the Control's center: print it when something spins off to the side. For example: const SpinPivot := preload("res://spin_pivot.gd"), SpinPivot.keep_centered($Radar), then turn it with $Radar.rotation += speed * delta.
extends RefCounted
## Spin and scale a Control (a HUD ring, radar, icon or button) around its own center
## instead of its top-left corner.
## Puts the pivot in the middle now, and again every time the Control changes size.
static func keep_centered(control: Control) -> void:
control.pivot_offset = control.size / 2.0
if control.has_meta(&"_pivot_centered"):
return
control.set_meta(&"_pivot_centered", true)
control.resized.connect(func(): control.pivot_offset = control.size / 2.0)
## How far rotation and scale have moved the Control's center on screen, in pixels.
## 0 means it turns and grows in place.
static func center_drift(control: Control) -> float:
var own := control.get_global_transform()
var parent := own * control.get_transform().affine_inverse()
var laid_out := parent * (control.position + control.size / 2.0)
var drawn := own * (control.size / 2.0)
return laid_out.distance_to(drawn)
What we measured
A ColorRect at (380, 170), 200 by 200, on a CanvasLayer, so its center sits at (480, 270). We compared where its corners were drawn with where its layout puts it. Two runs gave the same numbers.
- Default pivot (0, 0), turned 90 degrees: drawn center (280, 270), 200 px off. Turned 45 degrees: 108.2 px off. Scaled 1.5 times: 70.7 px off, toward the bottom right.
- pivot_offset set to the middle once: 0 px when turned. Resized to 300 by 300 and turned 90 degrees: 100 px off, because the pivot stayed at (100, 100).
- keep_centered(): 0 px turned, 0 px after the resize, 0 px scaled 1.5 times. Called twice, the resized signal had 1 connection.
- pivot_offset_ratio (0.5, 0.5), which Godot 4.7 has: 0 px turned, and 0 px after the resize.
- center_drift() gave the same numbers as the drawn corners in every case.
Why it happens
Every Control turns and scales around its pivot_offset, measured in pixels from its top-left corner, and the default is (0, 0). A Node2D or Sprite2D turns around its own origin, which for a centered sprite is the middle, so the same rotation code works there and not on a TextureRect. pivot_offset is a fixed number of pixels, so anchors, containers or a new screen size that change the Control's size leave the pivot where the old middle was.
What to change
- Turning forever: keep the angle small so the speed stays even after hours, see spinning node speeds up or slows down after hours.
- A pulse or pop-in on a button: the same centered pivot makes scale grow from the middle.
Blips on the radar once it spins: radar blips that turn with the player.
Spinning rings and radars ready to drop in: Godot 4 Ready Kit includes 124 Sci-Fi HUD Elements, whose rings, spinners, radars and reticles carry a spin script that sets the pivot to the middle and again on every resize, the same way as this page.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Godot 4 Ready Kit is 9 products in one bundle, including 124 Sci-Fi HUD Elements whose rings, spinners and radars turn around their own center at any size.
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
- 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
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total