0 votes

I added a canvas layer to the player scene trying to get a HUD to work, i've tried everything i could think of from making it a sprite which follows the camera but then leaves the top of the screen and goes past the camera limits anyways, i tried coding it so that it would move away from the player as it moves to the edges of the screen which didn't work properly either.
And even then i'm not sure if canvas layers can work with sprite sheets which is how i was gonna do the health system instead of hiding certain images based on the health and max health.

The camera is set to current and the only code i have for it is room detection and setting lmits which doesn't seem to be the issue

enter image description here

enter image description here

enter image description here

Godot version 3.5.1
in Engine by (12 points)
reshown by

2 Answers

–1 vote

Make your hud a child of the main camera, and that should work. Otherwise there's a property on the CanvasItem called follow_viewport_enable you can toggle in the inspector which may solve your problem.

by (123 points)
0 votes

The way I got my HUD to follow the camera was by putting the HUD elements into a Control with MarginContainer. My UI scene has the following layout:

LevelUI (CanvasLayer - layer is above everything else and has "Follow Viewport" off)
    -Container (Control)
           --MarginContainer (MarginContainer)
                  ---HBoxContainer (HBoxContainer)
                          ----Label (Label - just to control gap from the edge of the screen)
                          ----TextureRect (TextureRect - spinning coin)
                          ----VBoxContainer (VBoxContainer - so I could adjust the height of the Coin text)
                                   -----CoinLabel(label - which I control via script)

I worked the settings until I had an animated coin that would spin in the upper left corner of the screen with a label to the right of it that would update as coins were collected out of total coins in the level (ie. 1/20 coins).

by (23 points)
edited by
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 [email protected] with your username.