Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

2D Sprites in 3D world, depth sorting issue

Discussion in 'World Building' started by therobby3, Feb 1, 2019.

  1. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    Hello folks, I'm new to Unity as I am starting to transfer a large Flash MMO game to Unity.

    The game is a 2.5D perspective game. It uses an oblique perspective, which is similar to isometric. (think Earthbound perspective). I've been learning Unity for the past week and have begun starting to rebuild my game world in it. I think Unity's 3D will fit my game well instead of trying to build everything in an purely 2D engine, and trying to sort and do things that way.

    It's been working out well however now I am having some depth sorting issues. So right now I only have a player and ground tiles. Both the player and the ground tiles are sprites, meaning game objects with the SpriteRenderer component attached to them. I used the SpriteRenderer for the ground tiles, because they have a nice 9 Slice feature that I'm using, which keeps the cartoony outline on the edges and scales the grass texture in the center.

    Anyway, the issue is that I notice Unity is handling these "sprites" a bit different than a normal 3D object sorting wise. If I use a 3D plane as the ground with a texture, the player sprite simply disappears into it. However with 2D objects Unity seems to say the sprites must be either "above or below" each other, no in between. And it doesn't seem to be doing this exactly right. If you see my attached video you will see what I mean. The player is walking on a large ground tile and becomes sorted behind it all of a sudden, when it shouldn't be because it is above it. I'm not sure the exact calculations Unity is running to figure out the depths, but surely there is an easy fix for this?

    I've looked up some tutorials, but what I mostly get it about sorting on purely 2D games and using layers, which doesn't feel like it would be the right approach to this. Could anyone give some info? I've tried changing the cameras transparencySortMode which seemed to help some, but the problem still persists. Perhaps I would have to use 3D quads in replace of the sprites? Any ideas? See my video for the example.

     
  2. Noogy

    Noogy

    Joined:
    May 18, 2014
    Posts:
    133
    Sprites require that their sorting order be set, it doesn't matter where they are in relation to each other in the world. I know it takes some getting used to. My recommendation (and the way I've been doing it) is to create some simple tools to easily swap sprite sorting orders with a shortcut.
     
  3. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    Aw bummer, I didn't really want to mess around with any custom sorting code. Perhaps I'll have to try and see if using 3D quads with textures mapped to them will work better.
     
  4. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    Sorry, double post. I think I've sort of found a solution to this problem. It seems a majority of this problem lies in the fact that Unity can't correctly sort objects with partial transparency. And in the case of most 2D games, most sprites probably have partial transparency because of the pixels around edges and such. It doesn't sound like this is entirely a problem with Unity though, it's just a hard problem to tackle in general, and so Unity handles partially transparent objects as either being in front or in back of each other, no in between. And because of this, it can mess up sorting sometimes.

    So to fix this, I changed the sprites shaders to Unlit -> Transparent Cutout. This makes it so pixels in the sprite are either fully opaque or fully transparent, no in between. And because of this Unity can sort the objects using a better sorting approach.

    This seems to have fixed my issue for now. Whether it causes any issues down the road or not, we will see. Hopefully that helps too if anybody in the future finds this posts and has a similar problem.
     
    Nerfski and Spazius like this.
  5. Cabezotta

    Cabezotta

    Joined:
    Sep 19, 2014
    Posts:
    1
    Thanks robscherer123 that was a great solution for me!
     
  6. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    4,005
    you can set their sort in layer to be the same as the forward axis in your scene to automatically do this.

    So if your Z axis is forward/back in the scene, then you want to set it to be the same value as the sprites z value. This will mean that a sprite at 0 z will have 0 order in layer, and a sprite at 100 z will have 100 order in layer.

    You may need to flip this depending, if so add a - in front of it.

    This way you dont need to make everything transparent which will absolutely cause issues down the road, and is also more expensive.

    EDIT: to do this, just have it run a script which on start sets the sprite renderer "sortingOrder" field to be the same value as the objects Z transform value.
     
    camerontownsend likes this.
  7. HolyChocolate

    HolyChocolate

    Joined:
    Aug 18, 2021
    Posts:
    2
    Another Solution would be making the game using the Universal Render Pipeline.
    This at fixed the problem for me.
     
  8. nd_nomad

    nd_nomad

    Joined:
    Nov 18, 2023
    Posts:
    10