How to get mouse position based on Camera position and zoom?

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

Well i dont get it.

i can move my camera by clicking the mouswheel and draging the mouse
but also i can place tiles by clicking left mousebutton

now the tiles will appear annywhere but not there where the camera is.

this is what i wrote:

func _input(event):
if Input.is_action_just_pressed("mb_left"):
	pressed_at_position = get_viewport().get_mouse_position()
if Input.is_action_pressed("mb_left"):
	get_parent().get_parent().get_node("TileMaps/TileMap_Mauer").placeTile(pressed_at_position)

the problem is that icant use global mouse position. dont know why. but he sais there is none such function in base viewport

i dont have a idea how to put my tiles where i realy click at

Is this in 2D or 3D?

SIsilicon | 2019-09-24 01:50

It’s in 2d :slight_smile:

gruen | 2019-09-24 04:18

:bust_in_silhouette: Reply From: markopolo

get_viewport().get_mouse_position() gives you coordinates where 0,0 is the top left of the screen, not the origin of the scene tree. get_global_mouse_position() is probably what you want - what is the exact error message you receive when trying to use that?

(relevant question that gives an out of date answer)

1 Like