Search Unity

Is there a better way to give a different "Order in Layer" to different parts of the same sprite?

Discussion in '2D' started by Metsuryu0, Feb 8, 2019.

  1. Metsuryu0

    Metsuryu0

    Joined:
    Feb 8, 2019
    Posts:
    2
    I'm making a top-down 2D game, and I have some large sprites where I want the player to be drawn above them, if they're in front of them, and below them if they're below them, like this: Bonfire sorting.png .

    I've searched on how to do it, but couldn't find anything, so I figured out how to do it myself, but I think it's a very inefficient way, especially if I'm going to use a lot of sprites.

    To do it, I use the same sprite twice, overlapped, with different "Order in Layer" for each, and add a mask to hide half of it for one layer, so by hiding the part below, even if it's above the player, it won't be shown, but not hiding the top part, will make it show above the player, covering it, so it works like seen in the screenshot.

    The problem is that I'm using two sprites for each element, which could consume a lot of memory for very large maps.
    Is there a way to do this using only one sprite, so I could halve the memory used?

    Edit: Also, doing it this way is glitchy when larger things than the player pass in front of it.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Select Edit > Project Settings > Graphics, change Transparency Sort Mode to Custom Axis, and set Transparency Sort Axis to (X=0, Y=1, Z=0). This tutorial has a good explanation: Sorting Groups and Transparency Sort Axis.
     
    Metsuryu0 and Geejayz like this.
  3. Metsuryu0

    Metsuryu0

    Joined:
    Feb 8, 2019
    Posts:
    2
    Works great, thank you.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Glad to help!
     
    Metsuryu0 likes this.
  5. Shlomito

    Shlomito

    Joined:
    Nov 6, 2018
    Posts:
    4
    I was having issues making this work with a tilemap, and I realized you need to change the Mode on the TilemapRenderer to "individual", if anyone was having the same issue.