How to transform screen position to global through a Camera2D?

In Unity, there is a Camera.ScreenToWorldPoint() method. How to achieve the same function in Godot?

It looks like there is a conceptional difference between Unity and Godot.

In Godot, Camara2D is a tool used to set the view of the viewports default canvas layer. In order to access the transform between the canvas layer and the screen, you need to get it via the viewport.

In order to convert screen coordinates to world coordinates (my guess would be, that you mean canvas layer coordinates, but you might also mean viewport coordinates; the word “global” is very ambiguous) please try:

canvas_layer_pos = (get_viewport().get_screen_transform() * get_viewport().get_canvas_transform()).affine_inverse() * screen_pos

I would like to point you to Viewport and canvas transforms — Godot Engine (latest) documentation in English which has some examples of the transform functions.

You should also be aware that the position of input events depends on the context, from which they are accessed.

1 Like