I am a long time (15 years) Game Maker User and just recently switched to Unity.
I am absolutely in love with unity right now…its awesome.
My focus will be on 2D games…and currently I am messing around with Unitys capabilities.
I am trying to make a top-down engine…like in Zelda - Links Awakening… its just a sandbox though…it wont become a game
Anyway, I wonder how I can place tiles in the room that the Player can be infront of but also behind when he moves “behind” it…from a perspective view I mean
I know about the Sorting Layer … but I am not sure what the best approach for this would be.
And, not only the player must be able to be infront / behind some tiles but also enemies n other moveable entities.
Basicly, the wall should either be infront of the player or behind…depending on where the player is standing.
Hope I explained well enough… thanks in advance
Edit:
Kinda got it working. I am now having each tile and the player change its transform.position each “Start()” and “Update()” (Update only if its not static).
The transform.position is then set with a new Vector3 but with the z value set to y… works…but I wonder if this is the best way to do this
Th way you are doing isn’t wrong. There are other ways of course, like changing sprite order instead of Z value. You can put your sprites in multiple categories by layer. Like for example have a layer for background stuff so you won’t end up going behind that and don’t need to set it at start, then have another sorting layer for everything that the character can go in front of, and put also the static sprites in that layer.
The problem in all of this, including your solution is that you need to attach a lot of components on many objects to set their depth at start.
The only other way I can think of is the more tricky and it involve having a depth map that auto calculate pixel depth from a greyscale texture. With this you won’t have to set anything at start, a depthmap will be sent to the z buffer of the camera and each pixel will be drawn in order (think of Baldur’s Gate or the first games of Resident Evil).
If you want to try this last method, I adapted a shader from this guy work (Pre-rendered backgrounds in Unity (2/2) – Rendering | Geometry saves the Earth!), to work with sprites. The depth map will not produce any drawcall, you still get 1 drawcall per sprite sheet, the only hassle is that you have to paint your heightmaps by hand, but considering your sprite size isn’t much of a trouble.
Thanks for the reply.
The way I got it running now works…but …yeah, might not be the most efficient one…however, the way you described doesn’t sound perfect either…
Each of my tiles now has a sprite renderer …I dont quite like this as they will propably never move or anything…still, I have no idea how much CPU this takes and how flexible unity is when it comes to 2D… any experiences with this?
The other method I describeed of course works also with sprites that has multiple rects. But anyway, yeah using many gameobjects for the layout isn’t a great idea. Sooner or later is gonna be a big overhead and the dynamic batching will put your performance down a lot, no matter if they are 1x1 pixels tiles.
You can solve this with many methods, depends on how you want to work with it. You can do a mesh composed by many quads with each uv set to the tiles you want, so this way you get one mesh for everything, of course this will break any dynamic object that need z depth with your solution, but still for the “terrain” and static elements you can still go for this.
Also another thing I forgot to mention, for elements like the tree top or stuff that will be always above the player, you can use the ordering in layer parameter on the sprite renderer instead of messing with the z of the transform. You can also add multiple Sorting Layers to group elements, take a look here too: http://unity3d.com/learn/tutorials/modules/beginner/2d/sorting-layers
The way I would do this is by having SpriteRenderers only for dynamic objects, so everything that moves and is animated, for the static parts I would use normal MeshRenderers with chunks of quads to avoid having too many vertices on screen at once, for example have chunks of 16x16 tiles, the ones offscreen will be culled and you still have a nice vertex count.
The downside to this is that you will have to make your own tools to achieve that, sadly there is no way to have SpriteRenderer to contain group of tiles in one gameobject, unless you build your map in photoshop and save it as sprite, and use the image as map, which isn’t modular as you would need one texture per map.