How to convert mouse position to screen coordinates in 2D? (Camera 2D zoom)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By damncoder

I move sprite around the screen by mouse using this code:


But, sprite position and mouse cursor is different (very). They becomes absolutely different while changing camera 2d zoom.

In 2.1 version i am used without any problems following code:


In 3.0 this code not working. Any ideas?


:bust_in_silhouette: Reply From: Bartosz

use get_global_mouse_position it will give you position in absolute coordinates that can be directly passed to sprite e.g.

 sprite.global_position = getviewport().get_mouse_global_position()

It works! Thank you!

damncoder | 2018-03-24 22:15

Now viewport have not method get_mouse_global_position()

If we child Node2D class

sprite.global_position = get_global_mouse_position()

or sprite is child Node2D class

sprite.global_position = sprite.get_global_mouse_position()

Jesus05 | 2018-08-14 09:56