Godot Version
4.6
Question
Hello,
I have a question about UI and “ingame” elements.
In many games, text is displayed when the some entites are hurt, for example. Sometimes we want to display some text or icon close to a world entity.
I’m trying to do this, but I don’t find a correct solution to the problem.
In many places I read that I have to create a Scene with Control node as root and a Label inside. Then attach, instanciate to a Node2D. For example, attaching it to a CharacterBody2D)
If I have a decent font size and after scaling down the whole control node, this will work but there is some problems that arise, as the text will go behind other elements.
I can solve this by increasing the Z-index, but then the “z-index” war will start, with myself trying to figure out the indexes for this.
The text element will go behind other UI Elements, which is good. But not always.
For that fixing that, we have the node CanvasLayer and I can also “fake the position” so it looks really good:
But here is where my problem start to shine.
What if we have a camera limit and/or the entity is not close to the anchor that we setted (for example, center)?
Then the text will keep its position to that anchor, obviously.
So I thought that maybe I can position the text via script and here is where I bumped into the wall.
I tried to position from the entity script with something like:
interaction_control.set_position(get_global_transform_with_canvas().get_origin().round())
But as you can watch on the video, the text will do a weird effect only when the entity moves vertically and not when moving horizontally.
Futhermore, now the text can leave the screen (also a problem with the first approach)
So now I wonder,
What is the correct way of positioning a UI element in a CanvasLayer relative to an entity of the game world?
How can I prevent it from leaving the screen?
Thank you for your time!
Kinds regards to everyone.




