How can I attach a UI to a 2D actor?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By brazmogu

I want to attach some UI elements to my 2D actors so I can have, for example, a health bar floating over my units. Is there a correct way to make my Control node’s position follow my actor’s position (which is what I think Unity refers to as “World Space UI”)?

:bust_in_silhouette: Reply From: kidscancode

Just make your Control(s) a child of the actor. It will follow automatically. If your actor rotates, you can reset the global_rotation manually to compensate.

I can link you to an example, if it helps:

Well, that was unexpectedly simple XD Anchoring it is a bit harder than it seems, but it does work…

Follow-up question: can a UI element be drawn behind the rest of the stuff in the game? I can’t find any Z field in the control nodes, and using a Canvas Layer is just making the element disappear :expressionless:

brazmogu | 2020-03-19 02:52

I’m currently facing this problem, and with this solution the HUD elements (health bar, etc) will appear behind other actors in the game. I’m working out a way to use CanvasLayer to make all the HUD elements appear in a separate layer above the game actors, while still following them.

vulpido | 2020-08-13 16:49

:bust_in_silhouette: Reply From: Merlin1846

Another idea is if your camera moves sepratly from the player would be to put the ui in a separate canvas layer, this would also make it so that if your in a dark room you would still be able to see your ui.