Get mouse position when Camera moved

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


I made a custom block builder in the world (like a tile map), and all was working good until I added pan to my camera. As I didn´t have any idea how to make it work I followed a tutorial from Kids Can Code to make the pan:

The problem is that now, when the camera moves I can´t get the block at the position of the mouse, there is always a big offset.
The code is simply:

if event is InputEventScreenDrag:
		blockToBuild.position = event.position

But when adding pan, I can´t get the block following de mouse/touch, I tried all this things:

blockToBuild.global_position = event.global_position
blockToBuild.position = get_mouse_position()
blockToBuild.global_position = get_global_mouse_position()
blockToBuild.position = get_viewport().get_mouse_position()
blockToBuild.position = make_input_local(event).position
lastBlockBuilt.position = to_global(event.position)
lastBlockBuilt.position = event.relative

And always happen when moving the camera with the pan. I thing the problem is in this line from the camera (I attached the link):

position += event.relative.rotated(rotation) * zoom.x
:bust_in_silhouette: Reply From: ipdramon

I have not tried this myself, but your camera is moving, so it should be at a new position/offset after moving, correct? Have you tried subtracting the cameras position from your mouse position? Maybe thats exactly the offset you are looking for.

Yes the problem is that the camera is changing the position, I mean I move the camera with pan, I stay the camera in the position where I moved, and there trying to build a block is where I am getting the offset because of the camera position. I already tryed substracting the camera position but the offset is still there. I think I have to do a calculation with the camera position but none of the things that occur to me, like subtracting and adding the camera position is working

Pelli | 2021-11-09 23:11

:bust_in_silhouette: Reply From: Pelli

Found the solution, I don´t understand why, but I have to substract not only the camera position, but also the inital position of the camera:

block.global_position = event.position - (Vector2(960,540) - Camera.position)

Same issue here, that’s super weird

Now, in another issue i found make_input_local(event).position and it works for me