How to keep player on the TileMap?

Godot Version

4.2

Question

Is there a simple way to stop the player from wandering off the edge of a TileMap in a top-down game?

The floor tiles have no collision geometry (because they’re the floor) and nothing stops the player from walking off into empty space.

I found a few Godot 3.x posts asking a similar question. The solutions always seem to revolve around adding a border of invisible collision tiles, or writing a script to programmatically build a collision boundary when the level is loaded. Has the situation changed at all in Godot 4.x?

well no, you have physics layer for that, you can draw a collision on that physics layer in your tileset’s tile. draw the tile or wall in Wall layer, so you have floor and wall layer
wall layer with those wall tiles that has “collider” or physics layer

1 Like

Are you suggesting that I could use the same floor tile, but assign it different physics properties on different layers?

well, dont have to, any other will do, just assign a shape for the physics layer in a tile and use that newly assigned tile with physics layer drawn from tileset. Then you put/draw this tile on Wall Layer
a blank/ dark tile will do for the wall texture

I think the simple answer is no.

Here are some things you can do from the top of my mind:

1. You can have a margin tile that acts as a wall. (I recommend this)
2. You can have a static collision node that acts as a wall.
3. You can have a script that teleports the player back to the closest tile.
4. You can have a script that prevents the player movement in a direction when he is on a margin tile.
5. You can check if the next position is on a tile before moving the player. (or this)
6. Have a margin tile that has a collision set. Is what @zdrmlpzdrmlp is proposing. Depending on your project this may be a great solution. Tho it takes a little bit of knowledge to set it up. And may or may not work with your specific case.

5 seems really great if 1 is not a good option for your specific case.

1 Like

That would be my goto.