Is there a way to get the rect2 of the camera, instead of the viewport?

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

I’m making a game where I want the player to be limited inside what the camera can see, and have the camera move around. However, when I use get_viewport_rect(), it only returns the viewport stuck at 0,0. Is there something I can do that gets the camera’s viewport instead?

1 Like
:bust_in_silhouette: Reply From: Hugo4IT

I couldn’t find any built-in function for it, so you’ll have to calculate it by making a Rect2 out of the camera’s position and the viewport’s size:

extends Camera2D

func get_camera_rect() -> Rect2:
	var pos = get_camera_position() # Camera's center
	var half_size = get_viewport_rect().size * 0.5
	return Rect2(pos - half_size, pos + half_size)

func _ready() -> void:
	print(get_camera_position()) # (403, 147)
	print(get_viewport_rect().size) # (1024, 600)
	print(get_camera_rect()) # (-109, -153, 915, 447)
1 Like