How to convert a GUI node's position to a game world position?

Hello everyone,

I think it should be noted that gui.get_screen_position() returns a position with the z component set to 0 but camera.screen_to_world() expects a position with the z component equal to the distance between the camera and the resulting world position( API reference (Camera) ).

This is especially important in the case of cameras with perspective projection.

In the special case someone wants to move a GO to the position of a GUI node, and uses a camera with perspective projection it would look something like this:

gui_node_screen_pos = gui.get_screen_position(node)
gui_node_screen_pos.z = camera_pos.z - go_pos.z -- distance between the camera and the GO along the z axis (default forward axis, assuming camera was not rotated)
new_go_pos = camera.screen_to_world(gui_node_screen_pos)
5 Likes