Search Unity

Question Best way to place a lot of trees in a tilemap

Discussion in '2D' started by Toreth, Nov 15, 2022.

  1. Toreth

    Toreth

    Joined:
    Apr 8, 2017
    Posts:
    31
    I'm placing trees on a 2D tilemap, so some trees are in front and should obscure the trees behind them (like in the attached image). So each tree's TilemapRenderer will need to have the appropriate "order in layer" value so that it obscures trees behind it and is in turn obscured by trees in front of it. Is the correct way of handling this to have every tree in its own Tilemap component? That seems like by far the easiest way of going about it, but I'm worried that having a lot of Tilemap components within my Grid might be a bad idea.

    The alternative is to have multiple trees on the same Tilemap, in which case I'd need some complicated logic to make sure that the Tilemaps all have the appropriate sorting order so that every tree in that Tilemap is drawn correctly relative to the trees in front and behind it.

    Is it OK to potentially have hundreds of small Tilemaps in my Grid, each of which has just a few tiles for one tree?
     

    Attached Files:

  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    In such situation why use tilemaps at all? Why not just create regular sprites and use the sorting axis to ensure that correct one is on top automatically without having to assign "order in layer"?
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    You want to change your TilemapRenderer from Chunk to Individual Mode. This allows each sprite on the Tilemap to be sorted individually against other objects and characters. This will accomplish what you want automatically without all the complexity of managing multiple tilemaps. You will only need 1 tilemap for all of your "tall" stuff, like trees.

    You also want a Transparency Sort Axis of (0, 1, 0) which ensures that sprites are sorted by their y-value. To set this, it's either in Project Settings > Graphics, or it's on the 2D Renderer asset if you use the 2D Renderer. This is automatically the default setting if you created a new URP 2D project.

    The floor, on the other hand, can use Chunk mode and should use a smaller/negative Order in Layer. Chunk renders more efficiently, but you lose the ability to sort individual sprites against a character for example.

    More on 2d sorting: https://docs.unity3d.com/Manual/2DSorting.html
    More on tilemap rendering: https://docs.unity3d.com/Manual/Tilemap-Isometric-RenderModes.html#individual
     
  4. Toreth

    Toreth

    Joined:
    Apr 8, 2017
    Posts:
    31
    The problem with this is that each cell in the tilemap can only have one tile, so all of my background trees would be erased when I draw a foreground tree in those tilemap cells. For instance, in the attached file, several of the tiles for the tree in back were overwritten by the front tree's tiles since both trees are in the same tilemap. Am I overlooking something from your answer?
     

    Attached Files:

  5. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    You want your trees to be a single sprite. Put the pivot point in the Sprite Editor down at the bottom of the trunk. With Individual mode, the single-sprite tree will then sort from that single pivot point.
     
    ChiuanWei likes this.
  6. Toreth

    Toreth

    Joined:
    Apr 8, 2017
    Posts:
    31
    Good idea, that's what I'll do. I guess I was overcomplicating things using the tilemap.
     
  7. Toreth

    Toreth

    Joined:
    Apr 8, 2017
    Posts:
    31
    Thank you, that gives me exactly what I'm looking for.