Dev Log: Camera Movement
The camera wasn't something I put a lot of though into when I first started on programming, so initially we had a very old school style camera that simply followed the player character:
Not too dissimilar from a lot of top down games in the 80's and early 90's.
The problem was that, at least in large flat areas, I didn't want the out of bounds areas to be visible. (Obviously, when the traversable area is smaller than the game's viewport, some out of bounds area will be visible.)
So the answer was to set up limits to how far the camera could move relative to the map, which left us with this:
Now, when the camera hits the boundary of the game area, it stops and allows the player to keep moving toward the edge. On the code-side, this is surprisingly straightforward in Godot. The camera itself is attached to the player node, and a special node inheriting Area2D (CameraRegion) is added to each level scene that also contains the left, top, right and bottom limits for how far the camera should move.
When the Area2D is entered, it calls a global (CameraManager) to emit a signal with the positions of the limits. The player node is subscribed to that signal and uses the passed positions to update the camera limits. All told, it's very little code to make that work.
CameraRegion.gd:
CameraManager.gd:
Player.gd:
That also means that I get Link's Awakening style camera lerps on screen transitions for free. I just have to add multiple CameraRegion nodes to a single level scene and as the player moves from one to another, the entered region tells the camera manager to emit a new set of camera limits:
The camera butts up against the local region (in this case, just an arbitrary X value) and when the player crosses the path, the camera jumps over to the new set of limits.
Of course, the jumping doesn't look very good so by enabling smoothing on the camera node, we're left with this:
So that's it. We're done. This is what it looks like in a real part of the game and not a contrived test level:
I can't believe how easy--
Loading a game from a save causes the camera to spawn on the scene origin and then move to within the camera boundary. And with smoothing on, you get this unintended lerp.
Begin cussing.
Okay, well it can't be too hard to fix that. I'll just disable camera smoothing whenever a new map is loading, then re-enable it after the load has finished. Except that didn't fix it.
(Yes, that is the same GIF, but I'm not going to record multiple videos of the exact same thing happening.)
So I figured I should wait a frame after load to re-enable smoothing. In the first frame after load, the camera should immediately (and invisibly) snap to the new boundary, and then smoothing will get turned on and then it's fine from there.
Then I said, "Ehhhhhh.... I'll come back to this."
That was three months ago. Today I fixed it. You want to know how I fixed it?
I wait FIVE frames after loading new map. Four doesn't work. It has to be at least five. And now it works:
Is it the right way to do that? Nope! Is there some underlying issue that if I fixed would obviate the need to wait five frames? Probably! But I don't care! My camera does all of the things (read: two things) that I need it to do and I'm happy with it.
(Also, the five frame wait happens after the level has loaded and the player has control, so it's not even adding five frames to the load time. It's theoretically invisible to the player.)















