![]() The most obvious solution is probably to try a perspective camera, but the screenshot above shows the problem. (This is hard to see in a static image, but try the demo above.) Ideally it should be possible to able to achieve this look without writing a lot of code, or changing the all the objects in the scene to make the camera work. The ground is viewed from a 30° angle with a 60° FOV to give it some subtle perspective. The sprites are all drawn parallel to the screen without any distortion. The next task is to render the correct projection. All of the sorting will happen automatically without needing to intervene with your own scripts. Put together, the sorting layer draws sprites from the ground up and the camera’s depth sorting draws them from front to back. ansparencySortMode = TransparencySortMode.Orthographic You can simply configure your camera in a Start() method. Fortunately, you can configure your perspective camera to sort by depth like orthographic cameras do. It's especially noticeable for large sprites like buildings or trees and can be very distracting. This doesn’t work with sprites since moving the camera left or right will cause them to pop in front on one another. Orthographic cameras already default to the correct sorting mode. However, for perspective cameras the default sorting mode compares objects by their distance to the camera's center. ![]() This really only matters for upright objects, and we want them to be sorted automatically by their depth in the scene. The remaining task is to sort sprites within the same layer. Since ground patches are always flat, plants and characters are always upright, etc, then the sorting layer never changes and becomes a simple matter of setup. The basic ground patches were drawn first, then the detail patches on top (gravel, fields, etc), and then finally the sprites that stood upright. Instead, we use sorting layers to draw sprites from the ground up, and use the orthographic camera sorting mode to depth sort sprites within the same layer. Also, the sorting order property provides very little precision (only 16 bits), which limits the size of the world. If the player walked past the middle of a patch on the ground, their sprite would disappear underneath the ground! Using sorting layers and orders isn’t ideal, because it requires updating the sorting order every time an object moves. Also, because Unity depth sorts sprites by their center, we couldn’t rely on the camera to sort Verdant Skies' sprites automatically either as some sprites laid on the ground and others stand upright. Moving objects along the z-axis would change their size and break their reflections and shadows. Since Verdant Skies uses a 3D perspective, it wasn’t possible to use z-value exclusively for depth sorting. Fortunately, though with a combination of several features a simple solution exists. Unity’s sorting support is somewhat limited for 2D games, and people have tried a lot of creative, though tedious attempts to work around it. Since most 2D games use alpha blending, Unity must draw the sprites in the right order without the benefit of z-buffers. The first issue is how to draw the sprites in the correct order. ![]() A link to our camera script is included at the end of the article in the resources section. For an example, try our interactive WebGL demo. However, with the right settings and a little matrix math, it is possible to get a classic projection without a lot of work. Secondly, the projection needed is not a basic perspective like the one provided by Unity, and it can look quite bad if you use one. First of all, Unity doesn’t really know how to depth sort sprites in a scene like this. Since we were using a 3D engine (Unity) we might as well save ourselves some trouble! With the default settings, Unity won't help you make a projection like this, and there are a few issues. On older, 2D hardware, it would certainly be possible to achieve a similar look simply by controlling the drawing order, calculating the position and scale of each sprite.
0 Comments
Leave a Reply. |