Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Making Tilemaps delete each other's tiles upon collision/overlap.

Discussion in '2D' started by joshvector, Jul 8, 2022.

  1. joshvector

    joshvector

    Joined:
    Nov 11, 2018
    Posts:
    2
    Hi everybody,

    I haven't posted on here before, so I hope it's okay to ask this on this forum. Previously I get all my Unity/C# answers just by searching google (and ending up here!) I'm still really green at making my own game, so I have a lot of gaps in knowledge.

    I'm making a top-down 2D Zelda clone that randomly generates mazes and maps. Thankfully I got the algorithm working, that was the really hard part! But now I want to start adding stuff to the rooms. I have a script that places two tilemaps in each generated room (either on the east/west or north/south) with layouts of trees that it then randomly moves around. It looks nice and cluttered now, so I'm moving onto the real difficulty: Water.

    Here's what I want to do.
    My rooms already select from a list of random designs, to which I'll be adding another one for a multi-screen lake. In this example, a lake would be drawn in one corner of the room and would extend outside of the room's bounds into three other adjacent rooms. I can do this quite easily already. My problem is, when it comes in contact with other trees, rocks, etc. I want those destroyed so when I later add a script that assigns a secret stairway to one of those trees, it doesn't pick an underwater tree for Not-Link to burn with his Not-blue-candle. But everything my scene does is spread across multiple rooms with multiple tilemaps. I tried messing around with collisions but haven't succeeded in getting two tilemaps to detect each other.

    TL;DR:
    What would be a good way to make a tilemap detect other tilemaps' tiles it overlaps, and have those overlapped tiles destroyed?

    Thanks for any support you can provide!
     
  2. joshvector

    joshvector

    Joined:
    Nov 11, 2018
    Posts:
    2
    is this forum not used anymore?
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Untested but this approach should work so long as your tilemaps are all positioned at (0,0,0). I assume that there exists a Tilemap[] for allTilemaps.
    Code (CSharp):
    1. // set this tilemap
    2. targetTilemap.SetTile(cell, someTile);
    3.  
    4. // clear other tilemaps
    5. foreach (var tm in allTilemaps)
    6. {
    7.     // check other tilemaps to see if they have anything there already
    8.     if(tm != targetTilemap && tm.HasTile(cell))
    9.     {
    10.        tm.SetTile(cell, null); // remove tile in that cell
    11.     }
    12. }