Godot Version
v4.3.stable.official [77dcf97d8]
Question
I need help with the behavior I wrote below. I have been trying for couple of days, checking the forums, reading Viewport and canvas transforms on godot help. But I still couldn’t figure it out.
What I want to achieve:
- A UI screen will open next to a Node2D node. Node2D node can be at anywhere visible on the screen.
What (I think) I need to figure out:
- Translate the global_pos of Node2D to screen space
- Then translate screen space position to InfoGUI canvas position.
Information about my scene:
- 2D Nodes is the parent for the Node2D that UI will open next to.
- UI screen element is under InfoGUI (CanvasLayer).
- Camera2D is the main camera. It can move and zoom in/out
I don’t see anything explaining what you’re trying to achieve vs what is actually happening.
The picture is only to show how my scene is setup. I didn’t add anything to show what’s happening as I tried so many different things, and none of them worked. I edited my question to make it more clear.
I finally found a solution that works for my setup. If anyone needs it for future, here it is:
# scene -> Scene2D node that the UI will appear on the same position with
# main_cam -> Camera2D node which is the main camera
# item_pos -> position of the canvas item (UI screen) that will appear on the same position with the Scene2D node
var screen_pos = scene.position * scene.get_viewport().get_screen_transform()
var item_pos = (screen_pos - main_cam.position) * main_cam.zoom.x