Get current camera?

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

How do I get the current camera in gdscript?

get_viewport().get_camera() returns null instead of my current camera in the scene.

get_viewport().get_camera() returns the current active 3D camera (or null if there is none), but I don’t know which method can be used to retrieve the current Camera2D node.

Edit: See https://github.com/godotengine/godot/pull/38317.

Calinou | 2019-07-08 20:44

Did you ever find an answer to this one? It doesn’t work for me either (2d viewport/scenes)

JayFi | 2022-03-11 01:00

:bust_in_silhouette: Reply From: jitspoe

This works for me. Are you calling get_viewport() from a node that is in the viewport? Perhaps you’re calling it from a singleton/global node or maybe a node that isn’t added to the tree?

Cameras is an array of all active cameras in your scene, so you should always have only one camera active and access the first element in that array. I hope this was useful! geometry dash

Della Addilynn | 2022-12-14 09:42

Yes, I agree with you

angelacow | 2023-02-24 08:01

I know it’s an old post, but in case someone has the same question: in Godot4 there are two methods for this:
get_viewport().get_camera_2d()
and
get_viewport().get_camera_3d()

Hope this helps!