0 votes

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")?

in Engine by (72 points)

2 Answers

+2 votes

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:
http://kidscancode.org/blog/2018/05/godot3_tanks_part6/

by (21,313 points)

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 :|

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.

0 votes

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.

Node2D
-PLayer
--cam
-CanvasLayer
--ui

by (569 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.