3

Suppose I have a lot of SpriteRenderers in the scene and I don't want to sort them by layers, but using z-axis instead: The bigger the z of the object, the further it is from the camera, therefore it should be renderer behind other objects whose z is smaller.

I tried this out with an orthographic camera and it seems to work fine, but my question is: is it guaranteed that an object with bigger z will be renderer behind an object with smaller z?

More formally: Let A and B be two objects with z-positions of ZA and ZB, respectively. If ZA < ZB, then A is necessarily in front of B?

There are no sorting layers, all objects have the same order in layer (but not necessarily the same layer). The camera is orthographic.

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • 1
    It depends. For only SpriteRenderers afaik yes! But they will e.g. **never** be in front of a `Screenspace Overlay` canvas ... You can always change the render order by having multiple cameras and render on top of each other ... – derHugo Aug 07 '19 at 05:10
  • [![Sample Scene](https://i.stack.imgur.com/pYYLw.png)](https://i.stack.imgur.com/pYYLw.png) > Let A and B be two objects with z-positions of ZA and ZB, respectively. If ZA < ZB, then A is necessarily in front of B? I just tried it like you said and it works. if z is smaller, it will be shown first. – Beautyfull Castle Aug 07 '19 at 05:12
  • 1
    well OP already said he tried and it works .. the question is will it work in **all** cases – derHugo Aug 07 '19 at 05:13
  • Normally `SpriteRenderer` won't be used under the Canvas. `Image` is under the Canvas. – Beautyfull Castle Aug 07 '19 at 05:20
  • So I think there's no other cases. – Beautyfull Castle Aug 07 '19 at 05:20
  • 1
    Yes. It will work. But your colliders are not expected to work since Z values are different. The core concept of 2D is to manage only 2 Dimensions. Why you want to use 3rd dimension even in 2D ? It can create problems at later stages. – Bhavin Panara Aug 07 '19 at 06:10
  • This is a good point to take in consideration, @BhavinPanara, I'll probably need to use colliders. – Daniel Aug 07 '19 at 17:28

0 Answers0