How to anchor Control to the Camera2D properly?

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

I’m trying to make in-game GUI that anchor to the game window and adapt to the may different window sizes. My current setup looks like this:
enter image description here
I’ve also set stretch mode to canvas_items, and aspect to expand in project settings
I’ve added TextureRect with Full Rect anchor preset set for demonstration purpose. What I want to do is to make sure that TextureRect will always be stretched, and fill the whole window, which works correctly if I don’t have any Camera2D nodes on the scene.
enter image description here
Unfortunately, when I try to move the camera, TextureRect will stay on its world position like this:
enter image description here
What can I do to make sure that TextureRect will always follow the camera in this setup?

I don’t understand what the problem is, and when i try to recreate your scene tree i get the desired results.

CollCaz | 2023-05-10 22:21

The problem occurs when I try to move around camera2d object. I’m using this camera to center it on the middle of the generated map. What I want is to make sure that the UI (or TextureRect in this example) will always follow the camera, and will always be anchored and stretched to the corners of the window. What happens is that UI will stay on the same position in the world space. Comparing this to the Unity UI: I want to achieve the same effect as if I have Canvas with Render Mode set to Screen Space - Overlay, but right now it behaves like it’s set to World Space.

Ret44 | 2023-05-11 01:25

It does this because your canvas layer is a child of the camera. If the CanvasLayer is a sibling rather than a child, it should always scale to the screen size independent of camera.

spaceyjase | 2023-05-11 15:11

That wasn’t the case - it happend either when CanvasLayer was a child of the camera or not.

Ret44 | 2023-05-11 21:16

:bust_in_silhouette: Reply From: Ret44

I found the source of the problem - for some reason I had Follow Viewport enabled on CanvasLayer. After switching this off, everything works as intended.