How do I get the center of the screen without using Camera?

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

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

something like this? (both are vector2)

OS.get_screen_size()/2
OS.get_window_size()/2

AiTechEye | 2020-01-31 12:49

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

BorisJohnson | 2020-01-31 13:04

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

kidscancode | 2020-01-31 15:34

They mean the window

Merlin1846 | 2020-01-31 23:55

:bust_in_silhouette: Reply From: Merlin1846

I tested this and it does work : )

OS.window_size.x/2
OS.window_size.y/2

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

OS.get_real_window_size().x
OS.get_real_window_size().y

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.

Calinou | 2020-06-29 12:39

:bust_in_silhouette: Reply From: BorisJohnson
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