So I create a world and have a tilemap layer named ground and a player node named P1. After using get_used_cells() to get all the tiles in the tilemap, I then pick one at random to place the player in the scene on top of a tile. I then use map_to_local to get the actual position of the tile to place my player. However, about half the time, the player ends up off the tilemap tiles.
What gives?
var tiles = ground.get_used_cells()
var random_tile = tiles.pick_random()
P1.position = ground.map_to_local(random_tile)
This is in 4.3 with Windows
It may be that the tile map layer’s local position has a different origin to the player’s local position so they are offset from each other.
You could further convert to_global(), then try setting the player’s global_position to the result.
var tiles = ground.get_used_cells()
var random_tile = tiles.pick_random()
var local_pos = ground.map_to_local(random_tile)
P1.global_position = ground.to_global(local_pos)
Good thinking. Unfortunately that’s not it. What I didn’t say in my first post was that I’m using the GAEA add-on to generate the tiles in the tilemap layers. For some reason, those walker generated tiles aren’t being used?! Or the tilemap layer global position is somehow off compared to the rest of the nodes.
Thanks again for looking at this.