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. Dismiss Notice

Question Layering sprites with tilemaps

Discussion in '2D' started by penguin137, Sep 22, 2023.

  1. penguin137

    penguin137

    Joined:
    Apr 19, 2021
    Posts:
    3
    I'm making a 2d game using tilemaps and want to make the player render in front of things when they're in front of them and behind things when they're behind them, as determined by their y position. I tried using a custom Transparency Sort Axis set to (0, 1, 0), but that appears to be working based on local position of the sprite instead of world position. Also, even if it did work, it would result in their hat (which is a separate sprite) clipping through objects above them. How should I handle this to make the player render correctly?
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    Keep the Transparency Sort Axis set to (0, 1, 0).

    There is a sorting mode option in the sprite renderers, set it to pivot.
    Once you have done that, all that matters is where the pivot of an object is in (world position.y) with respect to other renderers in the same sorting layer and sorting order. Renderers in other sorting layers or sorting orders will of course behave separately.

    If you need things like hats to always show up in front of the wearers head etc. you'll need to use a sorting group component. If the wearer is on Default layer with 0 sorting order, the hat will be on Default layer with 1 sorting order. The sorting group will be the parent of wearer and the hat. The sorting group will be on Default layer with 0 sorting order. You should position the parent gameobject with the sorting group component properly to sort the whole thing correctly against other renderers that are on Default layer with 0 sorting order. Check the example in the manual.
     
    penguin137 likes this.
  3. penguin137

    penguin137

    Joined:
    Apr 19, 2021
    Posts:
    3
    The sorting group part worked, although in the future I'll need to add additional creatures with their own hats and stuff. Is there a way to dynamically create these sorting layers?

    Also, I think I was wrong about the issue with the render order: I believe the tiles are rendering based on the tilemap center instead of their own position. Is there a way to fix that?
     
  4. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    you need to go on the tilemap renderer and set chunkmode to individual
     
    penguin137 likes this.
  5. penguin137

    penguin137

    Joined:
    Apr 19, 2021
    Posts:
    3
    Thank you! This was the solution.
     
  6. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    You dont need to dynamically create additional sorting layers; if there is more than one hat character, their sorting group component will all be in the same sorting layer & sorting order.

    If you are asking if it is possible to add the sorting group etc. in runtime, you can do it with
    Code (CSharp):
    1. var sortingGroup = AddComponent<SortingGroup>();
    2. sortingGroup.sortingLayerName=....//or use sortingLayerID
    3. sortingGroup.sortingOrder=....