How to move TileMap through dragging mouse?

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

I have a tilemap and want it to go with my dragging. Exactly as it is in Clash of Clans. I have no idea how to do.

It is unclear what you exactly want to accomplish. Do you mean move the camera along a TileMap in the scene?

Ertain | 2020-12-26 18:32

It would be better if the tilemap would move because else, I’d have to move everything (Control Nodes and so on) with the camera.

MaaaxiKing | 2020-12-26 19:00

:bust_in_silhouette: Reply From: Lopy

You can implement dragging with something like that :
func _unhandled_input(event):
. if event is InputEventMouseMotion:
. . if Input.is_action_pressed("drag_screen"):
. . . print(event.relative)
Note that you have to add a “drag_screen” action in your projects settings, Input Map tab. You could check for left click directly, but it would prevent your user from easily remapping afterwards.

You can try moving the TileMap and the Camera, to see which is more practical.

:bust_in_silhouette: Reply From: MaaaxiKing

It’s even easier than Lopy’s solution with a new action. It exists InputEventScreenDrag. Then, one must just enable “Emulate Touch From Mouse” in the Settings and combine event.relative with the current position.