Before you move the icon to unproject_position(), hide it when Camera3D.is_position_behind() says the head is behind the camera, or when the point is off screen. In our Godot 4.7 test a character standing 4 m behind the camera still got a point from unproject_position(): (410, 277), inside the 960 by 540 window, on the opposite side from where the character really was. The script below hid that icon and kept the icon of the character in front exactly on its head.

The code
Add a CanvasLayer to your scene and put a Control with this script under it. Give the Control a size, put your icon inside it (we used a Label) and set target to the character. head_height is measured from the character's origin, usually its feet. The icon's bottom centre sits on that point.
extends Control
## Keeps a 2D icon (an emote, a name tag, a health bar) over a 3D character's head.
## Put this under a CanvasLayer, add your icon as a child and set "target" to the character.
## It hides when the head is behind the camera or off screen, so the icon never shows up mirrored.
@export var target: Node3D
@export var head_height := 2.0 ## metres above the target's origin
@export var margin := 0.0 ## pixels the head may be off screen before the icon hides
func _process(_delta: float) -> void:
var cam := get_viewport().get_camera_3d()
if not is_instance_valid(target) or not cam:
visible = false
return
var head := target.global_position + Vector3.UP * head_height
if cam.is_position_behind(head): # unproject_position still returns a point here, on the wrong side
visible = false
return
var p := cam.unproject_position(head)
visible = get_viewport().get_visible_rect().grow(margin).has_point(p)
position = p - Vector2(size.x * 0.5, size.y) # bottom-centre of the icon sits on the head
What we measured
A 960 by 540 window, the camera 1.6 m up and 5 m back, looking forward. Each head point 2 m above the character's feet.
- A character in front of the camera:
unproject_position()gave (565, 213),is_position_behind()false. The script showed the icon with its bottom centre exactly on that point (0, 0 pixels off). - A character 4 m behind the camera: (410, 277), inside the window, and
is_position_behind()true. Placing a label there drew 1,226 yellow pixels for the two icons, twice the 613 of the one real icon. With the script: 613. - A character 30 m off to the side: (1542, 228), past the right edge of the window. The script hid it.
- When the character behind the camera walked round to the front, its icon came back. When a character was freed, its icon hid instead of erroring.
Two runs gave the same numbers.
Why the ghost appears
unproject_position() projects the point through the camera without checking which side of the camera it is on. A point behind the camera comes out flipped through the middle of the screen, so it can land anywhere on it, often near the middle where the player is looking. is_position_behind() answers exactly that question, so check it first, then check the point against the screen rectangle.
What to change
head_height: 2.0 suits a human-sized character with its origin at its feet. Lower it for a slime, raise it for a giant.margin: how many pixels past the edge the head may go before the icon hides, so a big icon does not vanish while half of it is still on screen.- Use one of these per character. The script hides the icon by itself when the character is freed.
Ready-made emotes: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 effects that float over your 3D characters' heads, in Stylized and Retro PSX looks, including Love Hearts, Surprise, Anger, Dizzy Stars and comic POW! and KO! pops. The comic pops and 10 emotes also come as 2D sprite sheets with ready SpriteFrames.
For numbers that pop up in 2D: floating damage numbers. A crowd that does not emote all at once: spread idle emotes across NPCs.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 ready-made emote and comic effects that float over 3D characters' heads, including Love Hearts, Surprise, Anger, Dizzy Stars and comic POW! and KO! pops.
More how-tos for Emote Bubbles & Comic Pops VFX for Godot 4: A crowd of NPCs that never emote or bob in step
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
- 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