Camera2d gets stuck when I try to dynamically change camera limits and its been troubling me for days

Godot Version

Godot 4.1

Question

I've been trying to write some code to make it so the camera limits dynamically change across a scene (like the limits stay confined to a room until a door opens and then those limits suddenly go past the door into the next area within the scene) but when I first change the camera limits upon upon loading into another scene and getting a signal to change the limits my camera instead of being in the area i want it to be and following the player it keeps going to this one corner and the camera's actual position goes outside of the area as well.

I've tried so many things to fix it from setting the camera's position to the players (which made the displacement problem even worse) to making the camera not a child of a player, making the camera zoom very small to fit inside the limits (which didnt work regardless of if i changed it before or during the signal emission or not), and messing with other crap like limit smoothing and stuff like that, to which none of it has been working or doing anything in terms of making progress in fixing my problem. All I have managed to figure out is that the code that changes these limits is responsible and even then I don't even know why.

I've attempted clamping the camera's position inbetween the limits but I don't know how to properly do that and find the globalposition of the areabounds that I want to clamp to, as I'm new to godot and dont know really how to get that stuff in the first place. I have tried looking for alternatives like scaling the camera limits based on the tilemap but even then all the stuff with finding the right bounds for the specific area I want in the tilemap have been so confusing and tedious and none of the tutorials seem to make sense and in general I don't think this method even is going to work for the type of camera work I want to do where the limits are easily assigned manually to specific areas which i cant even fucking do because none of it works right.

I've been trying so hard to make sense of this issue for several days now and all my progress on this project has been going completely nowhere and I've been just so frustrated and don't know what to do. If any of you could help me with this issue I'd be eternally grateful for it.

Here is the relevant Code, I hope its at least readable:

void Room2Camera()

    {

if (GetNode<RoomBussFile>("/root/RoomBussFile").IsFromEnterance1 == true) //checks if player enters room from one side or another and adjusts accordingly

        {




this.GlobalPosition = new Vector2(

Mathf.Clamp(GeneralPosition.X,left,right),

Mathf.Clamp(GeneralPosition.Y,top,bottom)

           );

this.Zoom = new Vector2(40.0f, 40.0f);

this.Zoom = new Vector2(40.0f, 40.0f);

this.MakeCurrent();

this.LimitLeft   = 450;

this.LimitRight = 460;

this.LimitBottom = 655;

this.LimitTop = 635; 





         left=   this.LimitLeft ;

         right =   this.LimitRight ;

         bottom=   this.LimitBottom ;

         top =  this.LimitTop;




ResetSmoothing();




        }

He made something like you but with static camara soo…

I appreciate the feedback but this doesn’t really help my problem as it talks more about changing rooms than actually changing the camera limits, to which I already know how to get the changing rooms thing to work anyways. This is more to do with issues with changing the Camera’s limit size and having the camera screen (/veiwport, just whatever part of the camera that you can see through) and position being in the right place when doing so, which the video doesn’t really touch on unfortunately.

what about using more then one camara with diferent limits setted up for the rooms?
ik this may not be the bast idea bc you have to add 1 camara for all rooms. : /

Might be convenient for when I have to change the room scenes specifically, as this problem has been happening when I set the limits when I change scenes, however I don’t know exactly if it will work for when I have to change the camera limits when I’m still in the same scene. I’ll probably try that and then see if the error happens again when I change the limits within the scene (though I have a feeling I’ll probably get the same issue again when I do it as the code that changes the limits seems mostly responsible for my issue)

Why do you have to change the limits dinamicly?
Isn’t it simpler to make a cam for evry rom with fixed limits for this room?
or do you change the room size? if so just keep the cam as is and live with the empty area : /

Well I want a section of a Room to be temporarily blocked off for a bit, and I want the camera to not go past that blocked off section of the room until it is unblocked and the player can go past it, to which I want the camera then to be able to go past it once the room opens up.

you can make it dark or something with a sprite node tho hide it :slight_smile:


Something like this?
https://www.youtube.com/watch?v=ZEG52P0Ybck

In my situation though I want the camera to be confined to this smaller area until the room opens up to a bigger room, and for my approach I don’t want the camera to be able to see this void around the area.

Specifically I plan to use an Area2d that appears a bit after this blockade opens up that changes the camera limits once the player enters the other area in the room

Oh, so it is just one room?
What obout adding a cut scene, wher the room gets showed, and while the cutscene is running you swap the camaras wher one camara has diferent limits or… just swap them.
For future post on this forum, i recomment to add a video, because ther can be infomation you miss, but helps other devs to help you, on solving problems.

Yeah the reason why I want my camera limits to dynamically change is because I plan to have different camera limits for specific areas in the room. as for the cut scene stuff I don’t want to show the player the entire room just yet, rather I purposely want the player to not see the blocked off area until its unblocked, to add like suspense or whatever. Also if you want me to send photos to get a better picture of what I mean I can send them

hm… i cant help you with this problem, but i stil recoment to make a video of this problem and edit it to the original/the first post for other people.

Alright, I’m gonna try your suggestion with the different Cameras for different rooms to see if that does anything though.

okay but be avare of me being kinda new to godot.
Edit: And add a video of the bug/camara bracking.

The main issue is that your camera limits are much smaller than the camera’s visible area.

LimitLeft   = 450;
LimitRight  = 460;   // 10 px wide

LimitTop    = 635;
LimitBottom = 655;   // 20 px tall

This creates a limit box that’s only 10×20 pixels. A Camera2D must fit its entire visible area inside those limits. If the limit box is smaller than the camera view, Godot can’t satisfy the constraint, so the camera gets pinned to a corner.

The limit rectangle should be at least:

  • width >= viewport_width / zoom.x

  • height >= viewport_height / zoom.y

Your limits should represent the actual world-pixel bounds of the room, not tile coordinates.

I also noticed another issue: you’re clamping GlobalPosition before updating left, right, top, and bottom, so the clamp uses stale values.

Finally, you don’t need to manually clamp the camera at all. Camera2D already clamps itself using its limits. Just set:

LimitLeft
LimitTop
LimitRight
LimitBottom

and let the camera follow the player normally.

Also, Zoom = new Vector2(40, 40); appears twice, so one of those lines can be removed.

For finding the correct limits, the easiest approach is to place two Marker2Ds at the room’s top-left and bottom-right corners and use their GlobalPosition, or calculate them from a rectangular CollisionShape2D.

I’m not sure if it’s exactly the Camera’s visible area being the problem as I’ve tried setting the Camera’s limit to be smaller and specifically upon changing the limits of the camera it only then goes into a corner out of bounds. To show what I mean (also I have to do 2 other separate replies with media to show fully what I mean because forum rules) this is how the Camera looks in the Editor while looking debug version of the scene where the limits I want to scale to (and a smaller camera veiw to automatically fit in those limits) are already present by default

this is what the camera looks like when running the preloaded debug scene (which is really zoomed in, but still you can see its in the bounds of the area)

and then this is what the camera looks like specifically when the camera limits are changed in code, where you can see its stuck to this corner out of bounds.

I’ve tried changing the camera size to fit before and it didn’t work, but if this somehow has something to do with that then feel free to let me know how
(Also if you are wondering why the graphics are so shoddy, it is because I plan to add actual textures later.)