Search Unity

Bug Sprites with different Z positions still gets mixed up while rendering game

Discussion in '2D' started by RemDust, Apr 15, 2023.

  1. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hello,
    Because of my generation setup, I'm trying to avoid Sorting Layer option and I keep everything on Default Layer with no Sorting Layer.

    I want to rely on the Z position of my sprites to render them properly, I read that a camera with perspective mode would do that. But for some reason it's not working, my sprites are close, yes, but they are not on the same Z position so why would they render on randomly/glitchy way ?

    I really don't want to use Sorting Layer if I don't have to,

    Any suggestion would be greatly appreciated :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hey Kurt,, thanks for jumping in !

    I'm using URP (well 2D Renderer) but I just don't understand how people can manage to work with Sorting Layers settings in a large perspective scene.

    Each time you need to place a new sprite, or even worst, moving one of them in the Z axis it's IMPOSSIBLE to see what you're doing !

    I mean let's say I have this Leaf that I want to move before the Tree, I can't see what I'm doing if I don't change the Sorting Layer before moving it, if not, no matter what I do with my Lead transform, I can't see it placed where I want.

    It seems crazy to me xD

    I'm working on a script to update the Sorting Layer of the transform that I'm moving in realtime but it seems overly complicated for such a usual task !
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    I agree with you sorting layer is completely unusable

    what you need to use is transparency sort axis and set it for z (0,0,1) or (0,0,-1)

    you can find it depending on your render pipeline its located in different places, most likely you should find it when you inspect your renderer asset

    make sure all your sprites are on the same sort order(default 0) and all on the same mask layer
     
    RemDust likes this.
  5. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    THANK YOU SO MUCH !

    It was driving me crazy...
    I've setup an editor scripts that resolves Z axis competition, with this settings you gave me I think I might be able to render this bloody scene correctly ;)
     
    karderos likes this.
  6. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hey bro ! @karderos
    The Z sorting is pretty cool with this new setup but how do you work with 2D Lights then, it requires sorting layers to be set isn't it ?

    3D lights won't work with the 2D Renderer ^^'

    What is the trick ? :D

    Thanks again mate
     
  7. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    no sorting available for 2d lights, sadly
     
  8. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    That's what I was afraid of...
    This is crazy !

    2023, not solution to create a nice 2d game in a 3d environment xD

    I guess I shouldn't be using the 2D Renderer at all ? Would URP be a better choice so I can use actual 3D lights ? But then the Sprites wouldn't be lit, right ? -_-
     
  9. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    not sure if 3d lights will solve it hmm well

    what I can show you is this article
    https://www.gamedeveloper.com/programming/graveyard-keeper-how-the-graphics-effects-are-made

    but myself I wouldnt dream to try this because I am not a pro coder
     
    RemDust likes this.
  10. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Very nice read !
    More suitable for 2.5D though (and they don't seem to be relying on 2D Renderer... )

    I curse the day I fell for this new renderer, should have shot me in the foot instead xD
    Everything is just more complicated
    - shader blur ? -> forget about it
    - parallaxe and lighting ? -> same

    Jeeeeez