- Home /
How to simulate 2D layers in Unity
So, I've been experimenting with making a 2D game in Unity.
Nothing unusual (I think)- fixed camera angle + Z-depth to simulate parallaxing.
The problem I'm having is that often I want to have several sprites on the same layer, but without them clipping into each other or having z-fighting issues if they move away from the center of the camera's view.
I place my sprites as transparency-enabled materials on vertical 2D planes. They're pretty small textures, but I often use a lot of them- for example, for a single character, I sometimes have upwards of 8 planes to represent the character's arms, legs, and face.
The first thing that came to mind was to solve it by adding a very small difference in the Z axis to each semitransparent plane to simulate "layers" and determine which one draws on top of the other, but this still seems to have issues depending on where the objects are in the camera's view. Shortening the camera clipping helps but doesn't solve the problem entirely.
Is there an elegant way to solve the problem? Is it possible to have multiple semitransparent planes in the same Z position and define a some kind of Z-sorting to determine what should be drawn in front?
Answer by Fattie · Jun 12, 2013 at 07:17 AM
Unity I bet is now probably the biggest platform for 2D games.
"The problem I'm having is that often I want to have several sprites on the same layer, but without them clipping into each other or having z-fighting issues if they move away from the center of the camera's view."
I think you have two problesm
(1) To be clear are you using an orthographic camera ?! Generally you must do this for 2.5D games
(2) "The first thing that came to mind was to solve it by adding a very small difference in the Z axis to each semitransparent plane to simulate "layers" and determine which one draws on top of the other" ..
you are a natural game engineer, because that is exactly what you do
Say you have a layer at 10 (perhaps the clouds), and a layer at 8 ("enemies") and a layer at 5 ("bullets", say).
In fact, depending on the nature of the game, let's look at the layer at 5. What you normally have to do is "microstack" that layer (again depending on the game etc etc), so each one has a slightly different Z.
If you read my boring novel about pools here ... long post on pools, please vote it up because I need moar points! ...in fact sometimes at the pool level you will move them around in micro-layers. (it's a bit of a nuisance really.)
So, you precisely hit the nail on the head.
Now when you say this "but this still seems to have issues depending on where the objects are in the camera's view" ... that's just wrong. You have some sort of bug or problem.
Again you are using orthographic, right? Even if not, you just have some woe if that is not working. That is a separate question, I encourage you to post a new question with images etc. if you are still having that problem.
Incidentally it's almost impossible to bother doing 2D in unity without 2DToolkit, it's great. It is very widely used. Before wasting a moment on 2D development, get 2DToolkit (and all its competitors) and have a real experimentation with them and decide which one to use going forward.
Hope it helps
Thanks! This actually helps a lot. $$anonymous$$y camera was -not- set to Orthographic, as a matter of fact, and this seems to have been the root cause for most of my problems. :)
$$anonymous$$any thanks again! Checking out 2DToolkit now :)
Funny, I once answered another very similar question to this but I can't find it now
http://answers.unity3d.com/questions/435408/object-shown-on-top-of-another-while-it-shoulndt-b.html
Notice it's a common issue that comes up, on this one search down to the guys talking about "0.1" and so on
http://answers.unity3d.com/questions/311570/bullets-spawn-behind-the-craft.html
Your answer
Follow this Question
Related Questions
2d games: Camera settings? 3 Answers
2D and distortion shader 1 Answer
Is there a way to change the order of the "Layers" Tab? 0 Answers