+1 vote

I need to find the center of the screen without referring to the camera object. getviewportrect() does not take scaling of camera.

in Engine by (37 points)

something like this? (both are vector2)


I have a camera in the scene that moves, but I don't have access to it. Your method doesn't work for me.

How can you not have access to the camera? It's in your scene tree in the location you put it.

They mean the window

2 Answers

0 votes
Best answer
func _get_viewport_center() -> Vector2:
    var transform : Transform2D = get_viewport_transform()
    var scale : Vector2 = transform.get_scale()
    return -transform.origin / scale + get_viewport_rect().size / scale / 2
by (37 points)
0 votes

I tested this and it does work : )


You can also try this though I haven't tested it :\

by (567 points)

get_real_window_size() will include the window borders in its calculations. You generally don't want to use it to calculate the center of the game window.

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 webmaster@godotengine.org with your username.