Clamping x and y on their own turns a round minimap into a square one: a target off to the top right lands in the corner, outside the circle, and the icon points at the corner instead of the target. In our Godot 4.7 test, with targets 1,000 px away in 72 directions, the icon sat up to 38.1 px outside a 100 px minimap and pointed up to 38.5 degrees off. Arrows at the edge of a 1920 x 1080 screen pointed up to 49.3 degrees off. limit_length() for the circle, and scaling the offset until it meets the rectangle for the screen, gave 0 px outside and 0.0 degrees off.
The code
Save it as edge_marker.gd and load it with const EdgeMarker := preload("res://edge_marker.gd"). For a round minimap: icon.position = centre + EdgeMarker.on_circle(offset, 100.0, 8.0), where offset is the target's position from the player in minimap pixels and 8 is half the icon. For screen edge arrows: arrow.position = screen_centre + EdgeMarker.on_rect(offset, screen_size / 2, 24.0) and arrow.rotation = offset.angle(). EdgeMarker.is_off_edge() tells you when to swap the icon for an arrow.
extends RefCounted
## Keeps a marker for a far-away target on the edge of a round minimap or of
## the screen, pointing the true way. Clamping x and y on their own pushes the
## marker into the corners: outside a round map, and turned toward the corner.
## icon.position = centre + EdgeMarker.on_circle(target_offset, 100.0, 8.0)
## arrow.position = screen_centre + EdgeMarker.on_rect(target_offset, screen_size / 2, 24.0)
## arrow.rotation = target_offset.angle()
## target_offset = where the target is from the player (or the screen centre),
## already scaled to the map's pixels.
static func on_circle(offset: Vector2, radius: float, margin := 0.0) -> Vector2:
return offset.limit_length(radius - margin) # same direction, never past the rim
static func on_rect(offset: Vector2, half_size: Vector2, margin := 0.0) -> Vector2:
var h := half_size - Vector2(margin, margin)
if absf(offset.x) <= h.x and absf(offset.y) <= h.y:
return offset # already inside: show it where it is
var sx := h.x / absf(offset.x) if offset.x != 0.0 else INF
var sy := h.y / absf(offset.y) if offset.y != 0.0 else INF
return offset * minf(sx, sy) # slide along the same line until it meets the edge
static func is_off_edge(offset: Vector2, radius: float, margin := 0.0) -> bool: # swap to an arrow icon
return offset.length() > radius - margin
What we measured
Targets 1,000 px from the player every 5 degrees, a round minimap of radius 100 px with an 8 px icon, and arrows on a 1920 x 1080 screen with a 24 px margin. Two runs gave the same numbers.
- Round minimap,
clamp()on x and y: the icon up to 38.1 px outside the rim and up to 38.5 degrees off. A target up and to the right landed at (92, -92), 130.1 px from the centre. - Round minimap, the script: 0.0 px outside and 0.0 degrees off. The same target landed at (65, -65), 92 px from the centre, on the rim.
- Screen edge arrows, clamp on x and y: up to 49.3 degrees off. A target far right and a bit down, at 18.4 degrees, got an arrow in the bottom right corner at 28.9 degrees.
- Screen edge arrows, the script: 0.0 degrees off and never past the margin. The same target got its arrow on the right edge at 18.4 degrees.
- A target already inside the map stayed where it was.
Why it happens
Clamping each axis on its own keeps every point inside a square. On a round map the corners of that square stick out past the circle by up to 41%, and a point pushed into a corner has a different angle from the target. limit_length() keeps the direction and only shortens the distance, so the point slides along the line to the target and stops at the rim. A screen is a rectangle, so the script works out which edge the line meets first and scales the offset to that edge.
What to change
- A radar whose blips should turn with the player: turning radar blips. Icons above heads that flip when the target is behind the camera: behind the camera.
- A radar ring that spins around its corner: pivot in the centre.
Rings, radar and frames to put the markers on: 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
- 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
- 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