Topic was automatically imported from the old Question2Answer platform.
Asked By
IndigoGames
So, there is, in unity, the function ScreenToWorldPoint() that transforms a point from screen space into world space.
Is there a similar function in godot?
get_local_mouse_position() will return the mouse’s co-ordinates relative to the node that you’re calling it from, while get_global_mouse_position() will return the mouse’s position relative to the current canvas layer.
Returns the 3D point in worldspace that maps to the given 2D
coordinate in the Viewport rectangle on a plane that is the given
z_depth distance into the scene away from the camera.
Pretty late to reply here, but if you need to convert from view space to world space in 2D, you can use Node.get_canvas_transform().affine_inverse() to get the view-to-world-space transformation matrix.
To convert a view space position to a 2D world space position, you can use the following code:
var view_to_world = YOUR_NODE.get_canvas_transform().affine_inverse()
var world_position = view_to_world * view_position
Similarly, converting from 2D world space to view space:
var world_to_view = YOUR_NODE.get_canvas_transform()
var view_position = world_to_view * world_position
If you mean, getting the object’s surface position where mouse is pointing, then I created a tutorial just for this. It uses ray-casting to do this under the hood.
i am late, but it will help people coming here in future.