How to restrict camera2d within area2D

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

How do I make a camera2d that follow player but only stay inside certain area such as area2d (camera seen from megaman or metroid). I would appreciate if you could teach me how to implement such camera2d. Red line is area2d and camera2d only stay within area2d.

I have looked at mega-man-engine available at github, as they keep camera2d within reference rect, but it was beyond my understand to fully understand how the camera2d is controlled with my few knowledge and experience in coding. I have also looked camera flip screen template published by chucklepie and thought this could be very useful for my project. However, it didnt work somehow, though I followed his tutorial.

:bust_in_silhouette: Reply From: zenbobilly

You can set the limits of the camera in both the vertical and horizontal in script. If you look at the Platformer2D demo, there’s a level script that limits the camera extents in _ready().

Thank you zenbobllly! Can it be used in area entered function??
Like setting a different value of camera.limit for each area, so that I can change the camera limit several times in a scene.

amaou310 | 2022-08-10 13:43

You can do that or you can move the camera along a fixed path using AnimationPlayer or defining a path. I suggest you read the Camera2D documentation to learn the properties you can modify to change the way the camera moves. You can also change its position directly in script.

zenbobilly | 2022-08-11 01:32