← Pixel Dreamer Studio

An icon over a 3D character's head, without the ghost behind the camera

Ask is_position_behind before you use unproject_position.

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.

A blue character on a green field with a yellow !? over its head, shown twice. On the left a second !? floats over empty ground beside it
Godot 4.7. Left: both icons placed at unproject_position, one of them for a character standing behind the camera. Right: the script below.

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.

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

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