Position of Camera2D to place in the center of screen

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

I have Camera2D with set up limits. I zoom in and move camera’s position around. When it reaches an edge of the screen movement stops, but position keeps going. So when I want to go to the opposite edge I have to wait for position to go back to the edge of screen. Maybe there is a way to move the center of camera or make a position to be always in the center of screen?

:bust_in_silhouette: Reply From: fhgaha

Should set margins to 0’s and use this to not go close to limits:

var left = limit_left + (offset.x*zoom.x) + get_viewport().size.x * zoom.x / 2
var right = limit_right - (offset.x*zoom.x) - get_viewport().size.x * zoom.x / 2
var top = limit_top + (offset.y*zoom.y) + get_viewport().size.y * zoom.y / 2
var bottom = limit_bottom - (offset.y*zoom.y) - get_viewport().size.y * zoom.y / 2

position.x = clamp(position.x, left, right)
position.y = clamp(position.y, top, bottom)