2
\$\begingroup\$

I'm pretty new to Unity and I just finished some courses and tutorial found in the official documentation. I'm now in the process to apply what I learned so far using different assets from the ones provided with the tutorial, to see if I understood all the material.

I'm trying to build a simple top-down game with some free asset I found online. My tile palette is composed, among others, of tiles like these:

tile palette

Now, in order to create something like this:

[sorry for the quality, I recreated it in gimp since I'm not on the computer where I have Unity now]

scene

I had to create three Tilemaps under the same Grid. The botton tilemap contains the grass (sort order -2), the middle one contains the fence (sort order -1) and the top one contains the house (sort order 0).

My first question is: given the tileset I have, is this the most efficient way to obtain what I needed?

After having solved this problem I wanted to add a collider to the fence and to the house, but since they are on two different tilemaps I cannot use a composite collider to optimize the collider's layout. Is there another way to do this in a more efficient way? Instead of using tiles for "interactable objects", should I use "proper" game objects instead?

\$\endgroup\$
2
\$\begingroup\$

given the tileset I have, is this the most efficient way to obtain what I needed?

Yes, stacking multiple layers of tilemaps is a tried-and-true method which is being used since the 16bit age.

But since they are on two different tilemaps I cannot use a composite collider to optimize the collider's layout.

That's incorrect. You can merge the TilemapCollider2D of multiple tilemaps into one CompositeCollider2D when you make both tilemaps children of the same Rigidbody2D and put the CompositeCollider2D component on that mutual parent. Just remember to enable "Used by Composite" on both TilemapCollider2D's.

But note that perhaps not all layers should have colliders. It's not uncommon to have one or more foreground-layers without collision. That allows the player to walk behind roofs or trees.

The object hierarchy would then look like this:

Grid [Grid, Rigidbody2D, CompositeCollider2D]
      Tilemap_Floor [Tilemap, TilemapRenderer, TilemapCollider2D]
      Tilemap_Objects [Tilemap, TilemapRenderer, TilemapCollider2D]
      Tilemap_Foreground [Tilemap, TilemapRenderer]
\$\endgroup\$
3
  • \$\begingroup\$ Thank you very much @Philipp! Regarding your note: I managed to make the player walk behind the roof by using the custom axis sort (set to Y), while limiting the collider area of the house, and it seems to work pretty well for now. Could this approach possibly lead to problems in the long rung that I'm not able to see now? Thank you \$\endgroup\$ – manoosh Jan 26 at 16:58
  • 1
    \$\begingroup\$ @manoosh That works too, but adjusting all those colliders manually could become tedious. \$\endgroup\$ – Philipp Jan 26 at 17:02
  • \$\begingroup\$ I understand, thank you again! \$\endgroup\$ – manoosh Jan 26 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.