Godot Version
4.2.2
Question
Just starting to learn Godot and I’m trying to add some UI elements, elements that don’t move when the player camera moves. I’ve read that CanvasLayer should fix this but when I use it it doesn’t work as I expect. I’ve created a very simple example to show this.
I create a Node2D at the root. Then a Camera2D with zoom behaviour. Then a Polygon2D representing the game part of the scene. Then a CanvasLayer, with a Control child, with a Panel child as the UI layer.
My expectation is that when I zoom then the game contents will change size but the UI content won’t. But that isn’t what happens (see screenshots). What am I missing? Thanks
Camera2D.gd
extends Camera2D
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _unhandled_input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
zoom = zoom + (zoom*0.05)
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_WHEEL_UP:
zoom = zoom - (zoom*0.05)
node.tscn
[gd_scene load_steps=2 format=3 uid="uid://cvjly8lqjlbgv"]
[ext_resource type="Script" path="res://Camera2D.gd" id="1_8c80c"]
[node name="Node2D" type="Node2D"]
[node name="Camera2D" type="Camera2D" parent="."]
script = ExtResource("1_8c80c")
[node name="Polygon2D" type="Polygon2D" parent="."]
polygon = PackedVector2Array(-92, -6, 2, 116, 140, 50, 118, -96)
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="Control" type="Control" parent="CanvasLayer"]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
[node name="Panel" type="Panel" parent="CanvasLayer/Control"]
layout_mode = 0
offset_right = 40.0
offset_bottom = 40.0