I've been trying to implement some light procedural generation into my game, nothing fancy. Just spawning rooms down in different areas in order to add some replayability. I watched a few videos and the prevailing wisdom seems to be creating room prefabs and placing them at points in the world which I have done.
Its at this point I want to differ my implementation than those online, I would like to at runtime remove tiles at the North, South, East and west parts of the walls like so.
My main problem is trying to figure out where these points are in the tile map, I have tried creating empty game objects and adding them as children to the Tilemap and taking their positions and then using Tilemap.SetTile(child.transform.position,null)
to remove the tiles, however the tile that ends up being set is always wrong and offset by a certain amount, I have tried to use Tilemap.bounds
and Tilemap.localbounds
to try calculate the width and height of the Tilemap in order to get the midpoint between 0-width and 0-height but the bounds of the Tilemap is actually way bigger than the space my tiles actually cover in the world, so that's also untenable. Using Tilemap.WorldToCell
or Tilemap.LocalToCell
also has not worked.
If this is impossible, or just way more complicated than its worth, are there other ways for me to draw rooms at runtime that dont involve me creating a big 2D of gameobjects with sprites and colliders because that'll just tank my performance.
Heres the Code I'm using to loop through all the children of my Grid and check if they contain the correct tag (North South East West) and then using that position to swap the tile
void clearTiles(string dir){
List<Transform> tiles = FindComponentInChildWithTag(this.gameObject,dir);
foreach(Transform t in tiles){
map.SetTile(new Vector3Int((int)t.position.x,(int)t.position.y,0),tile);
}
}
public static List<Transform> FindComponentInChildWithTag(GameObject parent, string tag){
Transform t = parent.transform;
List<Transform> children = new List<Transform>();
foreach(Transform tr in t)
{
if(tr.tag == tag)
{
Debug.Log(tr.name + " " + tr.localPosition + " " + tr.position);
children.Add(tr);
}
}
Debug.Log("Children size " + children.Count);
return children;
}