0
\$\begingroup\$

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.

Image of room prefab

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.

Mock up of walls being removed from room

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;
 }
\$\endgroup\$
1
  • \$\begingroup\$ Can you show us the code you use to place your rooms and remove your walls, and a few concrete examples of "I try to remove this tile here at (4,3) but instead it modifies this tile at (5, 7)"? — these specific numbers can help us identify the source of the offset and how to correct it. \$\endgroup\$
    – DMGregory
    Feb 5, 2021 at 13:11

1 Answer 1

0
\$\begingroup\$

So I ended up getting it working, I had so many different issues trying to use so many different ways to find out the correct implementation, I had an extra offset in my map.SetTile() call which was putting me off!

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Can you show us this offset and how you diagnosed and fixed it, so this answer can be useful to future readers who find themselves in similar situations? \$\endgroup\$
    – DMGregory
    Feb 5, 2021 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .