0 votes

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 getviewportrect(), it only returns the viewport stuck at 0,0. Is there something I can do that gets the camera's viewport instead?

Godot version 3.4.2
in Engine by (103 points)

1 Answer

+1 vote

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)
by (104 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 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.