How can I make something like 2D depth? I tried using order in layer but it's confusing and I dont want everything to be on the same layer, so I thought there could be a better way, maybe using the perspective camera. I want something like this:
One way would be to layout the game in 3D space. You setup the camera to look down at the ground plane, and have all the sprites like paper figures tilted up at the camera. Then as your character moves around on that plane, they will go behind things in 3D space. For that method you keep everything on the same sorting order and layer, and let Z position determine render order. Think "Dont Starve" style. Another way would be to use the character's Y position to update the sorting order or Z position accordingly. If the character's Y position is greater than another object's Y position, then the character is sorted behind it. If the character's Y position is below it, the player renders in front. Here is an example of the second method. It's written in JavaScript for the Phaser game engine, but it's a great visual example of what I'm talking about: https://phaser.io/examples/v2/groups/depth-sort
Another way is with Z-Depth compositing. Basicly the idea is to ahve an additional heightmap of the sprites where the color of the pixels from a greyscale determinate the z depth of the image to be drawn on top or below other things. With this technique you don't have to do anything on sorting layers and can keep all your sprites on the same z if you want, also fairly easy to set up for pixel art. Is the same technique used in Pillars of Eternity, you can see more here: ALso a nice article plus a test in unity of it: http://mehm.net/blog/?p=1218 The additional height map doesn't produce any draw call is only there for sorting, the downside is that you gotta do them by hand for such art.
Basically that's simulating the pixel fragments being in '3d' rather than 2d by creating a depth value for each pixel, and then the depth buffer handles the discarding or including of pixels based on whether they are closer or further than the closest surface, i.e. behind or in front. It does also mean though that depth is handled per-pixel so if the character themselves has per-pixel depth, it's possible that some parts of the character could be behind an object while others are in front, so you'll have to be careful.