Touchscreen-like scrolling using Camera2D

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

I am trying to make a Camera2D object movable using the mouse, in a fashion similar to touchscreen scrolling, but I am getting an issue where the camera seems to “teleport” towards the upper left corner of the window before normal movement occurs. Is it possible to correct this behavior? So far, I am using the following code:

extends Camera2D

var isActive = false
var oldPos

func _ready():
    pass

func _input(event):
    isActive = Input.is_action_pressed("_lmb") #left mouse button

if Input.is_action_just_pressed("_lmb"):
	oldPos = get_global_mouse_position()

if event is InputEventMouseMotion:
	if isActive:
		var newPos = oldPos - event.global_position
		self.position = newPos
:bust_in_silhouette: Reply From: SASUPERNOVA

I have found a solution to my problem. Apparently, the cursor was moving towards the upper left corner of the screen because of the fact that the origin of Godot Objects is not the center, but rather the upper left corner of the Object. So the solution to the problem is to offset that difference by adding 50% of the width and height of the control to the newPos variable like so:

var newPos = oldPos - event.global_position + (self.get_viewport_rect().size / 2)