Best way to implement moving and zooming for multiple camera2D

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

I have 4 camera2D.
I wrote a functional moving / zooming in one script which is attached to one camera2D, but i want to use this script for all the cameras without copy&paste.

What is the best way to do that?

Also, if i just copy and paste the camera script for each one, they work but for example if my current camera is Camera1 and i moving left, Camera2 is moving also.

Maybe its better to set up one camera only? and travel in pixels?

:bust_in_silhouette: Reply From: rossunger

you can attach the same .gd script file to multiple nodes…

If you want them to only be affected when they’re “current”, then in the script you need to check ` if not current: return" in whatever function is doing everything

:bust_in_silhouette: Reply From: rossunger

you can also make unique scripts for each camera that inherit from a base camera class… if you need to add custom code to each camera…

you do this by adding class_name at the top of your script file

extends Camera2D class_name MyBaseCameraClass

then in each camera’s script you can do something like
extends MyBaseCameraClass