Get position of current camera?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Elf_Ears
:warning: Old Version Published before Godot 3 was released.

Is there any way to get the global position of the current camera?
I need this so I can ensure that screen effects work properly, for example the “fade out” effect showing when my character enters a door anywhere on the map
Thanks in advance…

For posterity: Get center of the current camera2d? - Archive - Godot Forum

keke | 2017-02-13 03:41

:bust_in_silhouette: Reply From: tiernich

In a quick test here:

-Node2D
----camera

print(get_node("Node2D/camera).get_global_pos()) #do the trick.

check the help search in godot for more info…

This is not really the best answer because you assume you know where the camera is relative to the node. It no longer works if you have X cameras scattered in your level.

I want to get the current camera as well when an object explodes in my level, but the explosion script has no clue where the current camera node is.
I could create yet-another-autoload-singleton, but I try to avoid them as much as possible, they are evil (and it wouldn’t be possible anyways because the camera is inside the player, which is not iself a singleton).

There is CanvasItem.get_viewport().get_camera(), but it always returns null… why?

Zylann | 2016-06-25 17:34

Because get_camera() seems to return 3D cameras only.

Soaku | 2019-08-26 20:20

:bust_in_silhouette: Reply From: brunosxs

I was with the same doubt as you.

The fact is: there is no easy method to o so, like a get_current_camera()

What you could do is what I did, I keep track of the camera:

Add a script to your camera and keep track of what camera is the current by passing the reference of it to a global variable Globals.set("currentCamera", self)

If you want to make this automatic, add the code to the _process loop function and also, checking if the camera is current first…
It should look something like that:

_process(delta):
if is_current():
Globals.set("currentCamera", self)

so, you could get the pos or all the methods and info from it later by doing: Globals.get("currentCamera").get_pos()

I didn’t use this in the process function, I prefered to do the checking and swaping myself, but other than that, it worked flawlessly for me with everything I needed.

:bust_in_silhouette: Reply From: jitspoe

If you’re working in 3D, there is a get_camera() function on the viewport, so you should be able to do get_viewport().get_camera(). I don’t see something like this for 2D, though, but just in case somebody is searching for this like I was, here’s the answer to that.