#How do I get the right limit and left limit of my current screen or camera?

1 messages · Page 1 of 1 (latest)

solar moon
#

This is my code, basically I want that went this characters gets out of screen from the top it appears from the bottom, the camera is following the player so it should update at each frame the position they need to appear, but currently it seems that doesn't update properly and it doesn't follow the camera correctly the enemy appear and disappear from a static limit.

    global_position += vel
    global_position.x = wrapf( global_position.x, GameManager.gameCamera.limit_left, GameManager.gameCamera.limit_right )
    global_position.y = wrapf( global_position.y,  GameManager.gameCamera.limit_bottom, GameManager.gameCamera.limit_top )```
noble wolf
#

get_viewport().get_visible_rect() would give you the current bounds of the camera and where it is in the world as a Rect2.

solar moon
#

I don't see that function in the rect2 documentation, is not there?

noble wolf
solar moon
#

Ah sorry I read the wrong function

#

And now that I have the Rect2 how I get the limits? I'm checking the documentation but I'm not seeing how to do it

noble wolf
#

The Rect2's end property would be the right (x axis) and bottom (y axis) limits.

The position would be the left and top limits.

#

The end is just position + size

#

iirc this is in global coordinates

#

You can also just use the Rect2 methods to check whatever you need to, like using has_point() to check if something is inside of it.

solar moon
#

I'm having the same issue with this method it doesn't update the values I guess it's using its local cordinates and not global

noble wolf
# solar moon

Do you have any CanvasLayer nodes or something?
If the camera moves, this should also change.

#

Also, how is the character getting out of the screen if the camera is following them?

solar moon
#

The characters that moves are npcs, and the camera follows the main player, basically I want some summons that move around the player and when they get out of the screen appear from the other side

solar moon
merry pivot
#

I seem to recall that get_visible_rect doesn't actually incorporate any camera information.

This is the C# method I wrote to handle this issue in the past:

private Rect2 GetGlobalCameraRect()
{
    var viewport = GetViewport();
    var camera = viewport.GetCamera2D();
    var zoom = camera?.Zoom ?? Vector2.One;
    var center = camera?.GetScreenCenterPosition() ?? Vector2.Zero;
    var screenSize = viewport.GetVisibleRect().Size / zoom;
    return new Rect2(center - 0.5f * screenSize, screenSize);
}

And here's the GDScript that Copilot spit back for it:

func get_global_camera_rect() -> Rect2:
    var viewport := get_viewport()
    var camera := viewport.get_camera_2d()
    var zoom := camera.zoom if camera else Vector2.ONE
    var center := camera.get_screen_center_position() if camera else Vector2.ZERO
    var screen_size := viewport.get_visible_rect().size / zoom
    return Rect2(center - 0.5 * screen_size, screen_size)