developmentNaïca: Advices about layers and how to order them correctly

Recommended Posts

Balayne, Lead Developer: This article is a good technique for crossing management elements and their display.
Nevertheless, since the 2018.2 version of Unity, it is now possible to do it thanks to a new function: 2D Sprite Renderer "Sort by Pivot Point" (https://blogs.unity3d.com/2018/07/10/2018-2-is-now-available/).
So it's now very easy to do that with the pivot point whereas before we were blocked from the center of the sprite.

Building a 2D isometric game can put you in front of some issues you can’t figure right away. You will lose a lot of time and effort on this and it will bore you to death but today we want to help you with one of the issue we experienced: the Order in Layer.

Fellow developers, you may one day face this simple but yet annoying issue: how to order correctly your layers on your game engine (characters, creatures, environment) so that you can see them the way they are supposed to be?

Example: front, in between, back, in between, front.

First in a 2D game, layers work according to the Order in Layer: you can set it in every game engine or graphic library. By doing so, it will allow your engine to create a list of visual items to display in the order you want to.

In this case we are using Unity as game engine for Naïca and we will describe you the procedure to resolve this issue (this can be used on other game engines).

Order in Layer

Unity doesn’t have a system to handle automatically the Order in Layer, so you are the only one able to do it. Basically, Unity allows you to manage the Order in Layer “through the layers” (yes you will hear “layer” a lot of times):

It allows you to sort out more easily your different layers, but first you have to organize them in 3 differents type:

lower layer: the one that will always be beneath the player (ground, grass, stones)

middle layer: here is the player and every elements that he may cross (other players, creatures, trees)

The lower and upper layers are easy to use because in most of the cases you won’t have to display more than two or three elements at the same place: you will have to change their Order in Layer directly in Unity by changing the values on ‘sorting layer’.

On the middle layer you will have static elements (trees, rocks, buildings) and others in movement (players, creatures): so in this group of layers you will have to change dynamically the values of Order in Layer on the elements in movement.

You have to apply a very simple rule: the more your element is located toward the bottom (ordinate axis: Y), the more its Order in Layer will be high. For a character, the best way is to use its position at its feet and not at its center.

Elements in movement

Here is a simple script used by a lot of developers on Unity (you will only have to add your elements in movement) :

You may encounter an issue if you need high precision and your game use decimal value for the positions. In this case do not hesitate to multiply this value by 10 or 100 in order to as precise as possible for your positions.

Static elements

For the static elements that will cross the characters you only need to copy the Y position from the base of your element (just like the feet for the players) and paste this value into Order in Layer.

The problem we had with this system on Naïca is the amount of work you need to achieve: if you have a large world with a lot of forests and villages for example, you will change the Y position manually for every element and you may do several mistakes after doing 50, 100, 200 elements.

One of the solution is to apply the same technique used for the elements in movement by putting the right value at Order in Layer in ‘Start’ instead of ‘Update’ (as explained in the code before). But, this solution may not be the best one because you will end up with a total mess if you are working on a map.

Instead, you can use a script editor to automatically change the value of your element allowing Unity to directly change the Order in Layer without your intervention.

Share this post

Link to post

Share on other sites

Hi @Krikium !
Thanks for reading this post, this article does not reflect the work done on Naïca but introduces an example of how to solve a problem.
Which as mentioned above is no longer relevant in the new versions of unity .