Getting a camera to stay inside a tilemap


#1

Hi all. Currently I have a camera within my level that simply follows the player through a level. If I reach the edge of the level collection/tilemap, black is shown outside of it.

I would like to edit the camera so that if it reaches the end of the tilemap, it would stop before the black/outside of the tilemap is shown.

Can someone point me in the right direction?


#2

A simple implementation would be to use the known dimensions of the display (as defined in game.project) to limit the camera movement. You’d calculate a limit for each side (left, top, right, bottom)

Assuming the camera is pointed to the middle of the screen:

left limit = no less than screen width / 2
top limit = no less than screen height / 2
right limit = no more than map width (world coordinates) - screen width / 2
bottom limit = no more than map height (world coordinates) - screen height / 2


#3

If you have edges that are not rectangular, an option is to set up a frame of triggers around the camera, just outside the screen. Then add corresponding triggers at the edges of the level and have the camera stop and back out of the trigger when it detects a collision,


#4

I’ve updated my drag-to-scroll camera example so that it respects the bounds of the tilemap:

Demo: http://britzl.github.io/publicexamples/drag_to_scroll/index.html
Code: https://github.com/britzl/publicexamples/tree/master/examples/drag_to_scroll


#5

Thanks all. I think using the display dimensions is going to work best for my game. I wasn’t expecting this much feedback so quickly. This community is great :slight_smile: