Align camera so that player is on center of screen while keeping drag margins

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

Hello! I’m working on a 2D top-down game and recently stumbled upon a question: is there any way to reposition the camera after it have been “dragged” by moving to it’s drag margins? I want player to move freely within current camera view, but if player has moved to the edge of screen, move camera to this side and then if player has stopped slowly align so that player is in center of screen. Right now camera is moving just fine (with drag margins) but after moving the camera player is kinda offset off center.

P.S: camera node set as child of player node

enter image description here

Thanks in advance!

:bust_in_silhouette: Reply From: rossunger

on the camera’s _process() function:

if position != Vector2(0,0):

and then some code that steps you camara towards (0,0) such as:

position -= position.normalized()
if position.length() < 1: position = Vector2(0,0)