sortingOrder
sprites. In other frameworks, it may be a change in the order along the Z axis or the drawing sequence.sortingOrder
), or depth, or order in Z, i.e. the order in which they need to be drawn. In this case, we first draw all the columns of the first row, starting with the first column with sortingOrder
= 1.sortingOrder
= 7, and we move on to the next row. That is, each element in the second line will have a higher sortingOrder
value than any element in the first line.sortingOrder
value will overlap all other sprites with smaller sortingOrder
values.sortingOrder
in increasing order. The result will not spoil even if we change the rows and columns in places, as seen in the figure below.sortingOrder
in increasing order.sortingOrder
than any tile on the bottom. As for the code, to add upper floors, it is enough for us to shift the y
value of the screen coordinates for the tile depending on the floor it occupies. float floorHeight=tileSize/2.2f; float currentFloorHeight=floorHeight*floorLevel; // tmpPos=GetScreenPointFromLevelIndices(i,j); tmpPos.y+=currentFloorHeight; tile.transform.position=tmpPos;
floorHeight
value indicates the perceived height of the image of a tile isometric block, and floorLevel
determines which floor the tile belongs to.sortingOrder
to the first line completely, and then move on to the next one. Let's look at the first moving tile or platform that moves along a single X axis.sortingOrder
will be equal to 8, because there are 7 tiles in the first line. If the tile moves along the Cartesian X-axis, it will move along the “track” between the two lines. For all positions that he can occupy on his way, the tiles in line 1 will have a smaller sortingOrder
.sortingOrder
value, regardless of the position of the dark tile in its path. Since we chose the “first line” method for assigning sortingOrder
we do not need to do anything extra for moving along the X axis. This case is pretty simple.MovingSortingProblem
in the sources .sortingOrder
rolling tile based on the line it currently occupies. When a tile is located between two lines, it is assigned a sortingOrder
based on the line from which it moves. In this case, we cannot follow the ordinal sortingOrder
in the string in which it moves. This destroys our depth sorting algorithm.sortingOrder
. First a green block, then a pink block on the left, then a blue block, then a pink block on the right, and finally a yellow block. When moving to the blue block, we break the order only in order to switch to the "first columns" method.BlockSort
in this case solves our problem itself.) The solution in action is shown in the BlockSort
scene. private void DepthSort(){ Vector2 movingTilePos=GetLevelIndicesFromScreenPoint(movingGO.transform.position); int blockColStart=(int)movingTilePos.y; int blockRowStart=(int)movingTilePos.x; int depth=1; // for (int i = 0; i < blockRowStart; i++) { for (int j = 0; j < cols; j++) { depth=AssignDepth(i,j,depth); } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = 0; j < blockColStart; j++) { depth=AssignDepth(i,j,depth); } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = blockColStart; j < blockColStart+2; j++) { if(movingTilePos.x==i&&movingTilePos.y==j){ SpriteRenderer sr=movingGO.GetComponent<SpriteRenderer>(); sr.sortingOrder=depth;//assign new depth depth++;//increment depth }else{ depth=AssignDepth(i,j,depth); } } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = blockColStart+2; j < cols; j++) { depth=AssignDepth(i,j,depth); } } // for (int i = blockRowStart+2; i < rows; i++) { for (int j = 0; j < cols; j++) { depth=AssignDepth(i,j,depth); } } }
SingleLayerWave
, where I added an additional wave motion along the Z axis to the lateral motion along the “track”.BlockSortWithHeight
.tileZOffset
is the amount of movement along the Z axis of our moving tile. float whichFloor=(tileZOffset/floorHeight); float lower=Mathf.Floor(whichFloor);
lower
and lower+1
are floors requiring a special approach. The trick is to assign sortingOrder
both these floors together, as shown in the code below. This corrects the order and solves the problem of sorting by depth. if(floor==lower){ // , depth=(floor*(rows*cols))+1; int nextFloor=floor+1; if(nextFloor>=totalFloors)nextFloor=floor; // for (int i = 0; i < blockRowStart; i++) { for (int j = 0; j < cols; j++) { depth=AssignDepth(i,j,depth,floor); depth=AssignDepth(i,j,depth,nextFloor); } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = 0; j < blockColStart; j++) { depth=AssignDepth(i,j,depth,floor); depth=AssignDepth(i,j,depth,nextFloor); } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = blockColStart; j < blockColStart+2; j++) { if(movingTilePos.x==i&&movingTilePos.y==j){ SpriteRenderer sr=movingGO.GetComponent<SpriteRenderer>(); sr.sortingOrder=depth;//assign new depth depth++;//increment depth }else{ depth=AssignDepth(i,j,depth,floor); depth=AssignDepth(i,j,depth,nextFloor); } } } // for (int i = blockRowStart; i < blockRowStart+2; i++) { for (int j = blockColStart+2; j < cols; j++) { depth=AssignDepth(i,j,depth,floor); depth=AssignDepth(i,j,depth,nextFloor); } } // for (int i = blockRowStart+2; i < rows; i++) { for (int j = 0; j < cols; j++) { depth=AssignDepth(i,j,depth,floor); depth=AssignDepth(i,j,depth,nextFloor); } } }
BlockSortWithHeightMovement
scene. Thanks to this approach, our tile can now move freely along either of two axes, without destroying the depth in the scene, as shown below.Source: https://habr.com/ru/post/348138/
All Articles