Top down characters zIndex

Hi, the problem is that zIndex of the characters are defined by their spawn order in the map. So for example if I spawn character A and B in this order, B will always be over A.

Will it be possible that the zIndex is calculated with the y position of the sprite in he map without looping on every spawned characters on the map ?

You can see the problem in the screenshot below

image

Edit: To be exact, the “A” zIndex have to change whenever the gameObject y is below or above the gameObject “B” so that, in the screenshot the girl should be below the guy but only when her y is above the other character y

You would have to set depth when creating the sprite or later, probably by looping.

Or you could use a Container and sort them yourself by y.

@samme thanks, however, if I set depth the zIndex will not change when the gameObject y changes, am I right ?

I have edited my question so that is more clear how zIndex is supposed to behave

Yes, you have to set depth in any case. It’s not related to y on its own.