When creating a 2D RPG, maps tend to be made from painting a grid using a tilemap and also tend to be rectangular. Since we don’t care about depth, this means we can readily use an orthographic camera.
The nice thing about an orthographic camera is that is quite simple to use and we can easily relate between the size property of the camera and the squares of our map’s grid: the size of the orthographic camera is equal to half the height of the viewport. Knowing this we can compute and cache the equivalent “size” value for width.
Once we know what distance from the character/target is being rendered, we can offset the camera by a suitable amount, or more accurately set it at a distance equal to camera.size or our computed width size, whenever the character is too close to the borders of the map.
Here’s the result, the camera follows the player but does not show Out of Bounds parts of the scene: