Does anybody know how to randomly spawn the player (or anything else) on a procedurally generated platformer tilemap so that they don’t spawn inside the walls? I really need help about that. Sigh.
My idea is getting the unused places on the tilemap after the platforms have been generated, put them in a new list and then use that list to spawn the player but I don’t know how to do that. I’m not too knowledgeable at coding yet.
To make things a little more complicated, the code I use to generate the tilemaps involves using integers arrays and instantiating objects on a map, whatever the map, involves using Vector3.
Here is the code I use to generate the maps (thanks to Ethan Bruins on the Unity Blog and his articles about randomly generating tilemaps); several maps are generated on the top of each other. I collapsed the switch block because it’s just a list to choose from.
Maps generation code:
public void GenerateMap()
{
levelMapsList = new List<int[,]>();
//Work through the List of mapSettings
for(int i = 0; i < mapSettings.Count; i++)
{
int[,] map = new int[width, height];
float seed = mapSettings[i].randomSeed ? Time.time.GetHashCode() : mapSettings[i].seed.GetHashCode();
//Generate the map depending on the algorithm selected
switch(mapSettings[i].algorithm) ...
//Add the map to the list
levelMapsList.Add(map);
}
//Allows for all of the maps to be on the same tilemap without overlaying
Vector2Int offset = new Vector2Int(-width / 2, (-height / 2) - 1);
//Work through the list to generate all maps
foreach(int[,] item in levelMapsList)
{
Algorithms.RenderMapWithOffset(item, tilemap, ruleTile, offset);
offset.y += -height + 1;
}
}
/// <summary>
/// Renders a map using an offset provided, Useful for having multiple maps on one tilemap
/// </summary>
/// <param name="map">The map to draw</param>
/// <param name="tilemap">The tilemap to draw on</param>
/// <param name="tile">The tile to draw with</param>
/// <param name="offset">The offset to apply</param>
public static void RenderMapWithOffset(int[,] map, Tilemap tilemap, TileBase tile, Vector2Int offset)
{
for(int x = 0; x < map.GetUpperBound(0); x++)
{
for(int y = 0; y < map.GetUpperBound(1); y++)
{
if(map[x, y] == 1)
{
tilemap.SetTile(new Vector3Int(x + offset.x, y + offset.y, 0), tile);
}
}
}
}
Player generation code:
private void InstantiatePlayer()
{
int[,] map = new int[width, height];
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
if(map[x, y] == 0)
{
int spawnLocationX = x;
int spawnLocationY = y;
playerClone = Instantiate(playerPrefab, new Vector3(spawnLocationX, spawnLocationY, 0f), Quaternion.identity);
playerFollowCamera.m_Follow = playerClone.transform;
Debug.Log("instantiating player");
return;
}
}
}
}
Both work fine but the player spawns inside the walls of the tilemap on a regular basis. I need to replace:
int[,] map = new int[width, height];
by an array that contains only the unused places on the tilemap that was procedurally generated so that the player doesn’t spawn inside the walls.
I could just use a way for the player to regenerate the maps in game but I need a reliable code that can also generate objects on a map, obstacles, traps, and so on.
Thank you for your help if you can help me. ![]()
In game images where the player has spawned inside walls:





