Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to force sorting layers or z depth on quads in a 2D scene?

Discussion in '2D' started by keblight, Jun 15, 2017.

  1. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    Hi, I'm having an issue with quads in my 2D scene which when set with certain materials always seem to get drawn first and thus, behind all sprites in the scene. The quads have materials with render textures applied which are linked to cameras in the scene. When I use non standard shaders with the material ei. Twist or custom shaders the quads for some reason get drawn first. Other sprites including the player sprite are overlapping the quad. Even putting the z depth right next to the camera does not fix this. Just wondering if anyone has any ideas as to why this is and how to fix it?
     
  2. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    if you are using SpriteRenderer on the quads, you can use Order in Layer property in Sprite Renderer's Inspector to make the quad appear in front of other objects. The bigger the Order in Layer is, the more the quad appears in front of all the others.
    If you're doing this in code, you can access GetComponent<SpriteRenderer>().sortingOrder
    I guess MeshRenderer has this property too.
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  4. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    thanks for the responses guys, I have tried setting the sorting layer via script with no luck...yet. I will keep trying.
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    It is possible that a shader/material you're using has the render Queue set explicitly to render in front or behind certain object types.

    See what your material says here:
    Capture2.PNG

    Or if you have access to the shader source, check if it defines something like "Queue" = "Transparent - 1" or something other than a default value.
     
    bensibley likes this.
  6. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    this is interesting, I don't know how to check that queue actually, but maybe we are on the right track with this. It seems no matter what it always draws the quad first, underneath everything else. What should I do to get the material/shader to draw a little later/after the default sorting layer sprites/objects have been drawn? Thanks for your help btw.
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Using the default sprite shader, sprites will render in the Transparent queue.

    Since you say you're using a quad, depending on what material you apply, it will render before/after or alongside the sprites. In order for Sorting Layer and Order to apply, I believe the object must be in the Transparent render queue.

    https://docs.unity3d.com/ScriptReference/Rendering.RenderQueue.html
     
    Kermit0110 likes this.
  8. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    Hey that seemed to fix it! Thanks for the help I appreciate it.
     
    LiterallyJeff likes this.
  9. Kishore0525

    Kishore0525

    Joined:
    Dec 12, 2018
    Posts:
    3
    how to set the order for quads as sorting layer


    https://answers.unity.com/questions/1064940/unity-5-meshrenderer-and-sortinglayer-not-working.html


    public string sortingLayerName = string.Empty; //initialization before the methods
    public int orderInLayer = 0;
    public Renderer MyRenderer;



    Now add this below Method


    void SetSortingLayer()
    {
    if (sortingLayerName != string.Empty)
    {
    MyRenderer.sortingLayerName = sortingLayerName;
    MyRenderer.sortingOrder = orderInLayer;
    }
    }


    Call this above method in start() method

    This Works:):):)
     
    SebastianG1611 and LAKSHAYMAVIA like this.
  10. LAKSHAYMAVIA

    LAKSHAYMAVIA

    Joined:
    Aug 28, 2018
    Posts:
    27
    Thanks This worked for me