Tracking an external node with Camera2D

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

Hi,

Is there a way to make a Camera2D tracking a non-parent node ? I’m trying to make a view that could dynamically switch beetween objects in a scene.

Thank you.

:bust_in_silhouette: Reply From: eons

A way can be with a script on the camera.
Assign a target node to a variable to follow and on process do:
position = target.position

Another way that does the same but without camera processing, on the node that wants to be followed by the camera, add a RemoteTransform2D node, when that node wants to be followed by the camera do:

$RemoteTrnsform2D.remote_path = your_camera.get_path()
This node will do the translation for you, and some types and axis can be selected to disable some motions temporarily.

:bust_in_silhouette: Reply From: snk

Problem solved.

I just attached the camera (set in current) to a node that is tracking the object I want to focus on and it copies his position through _physics_process(). The specified object is stored in a variable and I wrote a setter to change it : this way I can choose the tracked object from anywhere.