how to move the camera with middle mouse?

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

I’m having some trouble with moving the camera2d with middle mouse button. What I did was get the mouse displacement vector and add the vector to the camera.position like in the below image

but the mouse is moving relative to the world map ( from (x0, y0) to (x1, y1) ). is there any other way to move the camera with the mouse such that the mouse always stays on the same position in world map (always (x0, y0) ) but camera move???

:bust_in_silhouette: Reply From: AlienBuchner

The sample is using the ‘Input Map’ in the ‘Project Settings’ (there ‘move_map’ is caused by ‘Device 0, Middle Button’). The script is used by the Camera Node ‘Camera2D’:

extends Camera2D

var fixed_toggle_point = Vector2(0,0)

func _process(delta):
	# This happens once 'move_map' is pressed
	if Input.is_action_just_pressed('move_map'):
		var ref = get_viewport().get_mouse_position()
		fixed_toggle_point = ref
	# This happens while 'move_map' is pressed
	if Input.is_action_pressed('move_map'):
		slide_map_around()

# slides the map around
func slide_map_around():
	var ref = get_viewport().get_mouse_position()
	self.global_position.x += (ref.x - fixed_toggle_point.x) / 80
	self.global_position.y += (ref.y - fixed_toggle_point.y) / 80

(alternatively here’s the non-sliding version of this script for a more editor-like map movement)

Hi! I have a question, How can I limit the movement, like: if position.x == x:
don’t move, but if the camera came back to the “movement area” the cam can move again,
THANKKS

ManiCus | 2020-07-13 18:44