While on the Godot Engine Facebook page, I saw a request for some assistance building a camera system like Super Mario Bros for the NES.
I figured I would need a
Camera2D node, that constantly updated the
limit_left property of the camera, to prevent it from panning left beyond a certain point. So, as the player moved to the right, the
limit_left is updated to an ever-increasing x-coordinate so that if the player tried to turn around and go back to the left, the camera would not pan further left than the updated
That works great!
As you can see from the video, the player can move right, and the camera moves with them, but when they try to move left, the camera does not pan back to the left. BUT, as you saw, the player can still move left and go out of view. We need to prevent that! It should be fairly simple to do by adding a new
StaticBody2D with a rectangle shape, that is “stuck” to the left edge of the screen.
- This is the
StaticBody2Dnode, with child
CollisionShape2D(required). I also added a
ColorRectto make it visible during play to see the collidable.
- This is the scene view, and you can see the white
ColorRectI added to the
Once that is in place, I also needed to add a script and attach it to the
StaticBody2D node. I want to make sure that its position is updated as the camera moved.
Playing the scene again demonstrates that I can move right, but I can no longer move to the left. Perfect!
Download the Godot project files located here
Background image in example by YoshiGo99 on DeviantArt. Downloaded from JoomShaper