Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Multiple layers of objects sorted through the Y axis (camera transparency sorting)

Discussion in '2D' started by enricogp, Mar 11, 2023.

  1. enricogp

    enricogp

    Joined:
    Apr 16, 2014
    Posts:
    4
    Hello,

    I'm attempting to make so that, in an ortographic game, objects/tilemaps interact with the player's Y position, making so that the player appears in front of them or behind them depending if the player is above or below the object in the Y axis. There are multiple threads in this forum about this, an even an official unity guide available at on https://docs.unity3d.com/Manual/Tilemap-Isometric-CreateIso.html . This does work when the player's sort order is based on it's pivot, and when tilemaps have their rendering mode set to "individual" instead of "chunk", but I have found that it presents big issues as well.

    The issue is the following: I'd like to sort how objects/tilemaps are rendered amongs each other, but I still want some of them to interact with the player's Y axis. However, in order to sort elements comparing their Y positions with the player's, it seems like they must all be at the same Layer and Order in Layer as the player. This makes it not possible to sort objects/tilemaps within themselves using different Order in Layer values but still make so that they are the same layer and order in layer when interacting with the player specifically.

    For example: in a scene that has tree objects and wall tilemaps, I'd like for trees to always render in front of walls, but also for the player to be able to go behind on in front of either walls or trees (or both at the same time) based on the Y positions of those elements. As in, the display order between tree objects and wall tilemaps is "trees are always rendered in front of walls", but the interaction between a tree and a player should be based on the position of the Tree and the Player compared to each other. However, as soon as I modify the render settings so that trees have a bigger order in layer than walls, the Y sorting between the trees and players no longer works since they are now on a different order in layer.

    The only solution I've found so far in regards to this is by using Sorting Groups. However, this would require all objects that should interact with each other with different Order in Layer values to be children of the same prefab. I've attempted to create an empty prefab in order to do this, but not only this would make the hierarchy messy but it also makes so that, when sorting with the player, the Y position of any children of the empty object is considered to be the parent's Y position, instead of the individual element's (tree/tilemap) position.

    Unfortunately, avoid using multiple elements at which the player may go behind or in front of the player would be quite limiting, since it's a stealth game and the player must be able to hide from other players by going behind multiple elements.

    Long story short: if the camera's transparency sorting using the Y axis requires all objects to be at the same layer and order in layer, how can I make sure that environmental objects render in different orders but that, in regards to the player specifically, they instead take into account their Y position (Y sorting)? As mentioned before, environmental objects/tilemaps do not have the same parent in order for me to use Sorting Groups, and if I create a parent for them in order to do so then the position of the parent will be considered for Y sorting when interacting with the player.

    Below is a crude representation of what I'm trying to implement:

    yaxissorting.png
     
  2. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    you must use the pivots to fix it, set the pivot on the bottom of the tree and the bottom of the wall and it will work as intended

    im sure you dont want the tree to render above the wall if the tree was behind the wall

    yaxissorting.png
     
  3. enricogp

    enricogp

    Joined:
    Apr 16, 2014
    Posts:
    4
    Thank you for the reply! Weirdly enough, I'm not able to reproduce the issue anymore since that day (perhaps because I rebooted my computer?). Even more strangely, the pivots should have been properly set up, and I had attempted to reimport unity assets in order to see whether that would fix the issue. Perhaps this was some sort of a bug, although it is also possible that I'm missing something. Regardless, I'm grateful for your help, and if the issue ever occurs again I will keep in mind to double check the pivot of the objects (or sprites, in the case of tilemaps set to render individually) in the scene.