+3 votes

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....

in Engine by (69 points)

3 Answers

–5 votes

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...

by (429 points)
edited by

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?

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

+1 vote

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.

by (231 points)
+3 votes

If you're working in 3D, there is a getcamera() function on the viewport, so you should be able to do getviewport().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.

by (40 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.