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 Sorting layer breaks the sorting

Discussion in '2D' started by wannaMakeAGame, Aug 2, 2023.

  1. wannaMakeAGame

    wannaMakeAGame

    Joined:
    Dec 7, 2020
    Posts:
    47
    Hey, I have a 2d game that I use sorting layers for sorting, but I use drawmeshindirect to draw projectiles in my game, so they are not sprites, I set their order with queue order, it works correctly with sprites in default ordering layer, but it overrides the ones on other ordering layers. What is the issue and how to fix it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short, as far as the Standard Rendering Pipeline,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    For instance, you may even use multiple co-located cameras to more-explicitly control apparent draw ordering.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html

    Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.
     
  3. wannaMakeAGame

    wannaMakeAGame

    Joined:
    Dec 7, 2020
    Posts:
    47
    I see, I assume meshes drawn with drawmeshindirect is in the first category, but how can I add sorting layer to this, I see you can assign it a layer but nothing about the sorting layer. Also I read you can assign a sorting layer to casual renderer too https://forum.unity.com/threads/drawing-order-of-meshes-and-sprites.212006/#post-1425463
     
  4. wannaMakeAGame

    wannaMakeAGame

    Joined:
    Dec 7, 2020
    Posts:
    47
    Also, another problem, when I rotate a 2d object in x or y axis, 2d order doesn't work and part behind the supposedly background object not drawn. I want z position of the object to not affect the ordering at all, but it does when z position of the object changed? I had to make zTest off in shader, but it creates other problems with ordering. Is there a way to completely deactivate distance from camera ordering