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:

My_Sprite.set_global_position(get_viewport().get_mouse_position())

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:

My_Sprite.set_global_pos(Camera.get_global_mouse_pos())

In 3.0 this code not working. Any ideas?

Thanks,

: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