Help getting the canvas layer to move with the camera

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

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

:bust_in_silhouette: Reply From: TheIronGod

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.

:bust_in_silhouette: Reply From: inaruslynx

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).