Godot distance between 3d and 2d nodes

If you want the position of a 3D object on screen, you can do the following:

var position_2d = get_viewport().get_camera().unproject_position(position_3d)

That is, we use the camera projection to get the 3D position into screen coordinates with Camera.unproject_position which takes a Vector3 and returns Vector2. Why is that unproject and not project? Don’t know.

However, we need a Camera to do that. Luckily Viewport.get_camera returns the current 3D Camera. Why is there not a 2D counterpart? Don’t know.

And of course, to use get_camera we need a Viewport. We can get the Viewport the node belongs to with Node.get_viewport.

Of course, once you have the 2D position on the screen, you can use distance_to to find the distance to any Control on the screen, or whatever you need.