Godot Engine Example: Super Mario Bros Camera

by Carl Furrow — on  ,  , 

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 limit_left value.

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.

Godot UI showing the scene tree
  1. This is the StaticBody2D node, with child CollisionShape2D (required). I also added a ColorRect to make it visible during play to see the collidable.
  2. This is the scene view, and you can see the white ColorRect I added to the StaticBody2D

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

 Want to get updates in your inbox? Sign up to receive the newsletter!
Carl Furrow's photo Author

Carl Furrow


Addicted to learning and looking to master the art of solving problems through writing code, while occasionally yelling at computer screens.