+1 vote

Hi,

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:
http://kidscancode.org/godot_recipes/2d/touchscreen_camera/

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
Godot version 3.4
in Engine by (156 points)

2 Answers

0 votes
Best answer

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)
by (156 points)
0 votes

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.

by (189 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.