Search Unity

Question Issue with rendering objects in the right order in a 2.5D top down game

Discussion in 'General Graphics' started by thooom, Sep 15, 2021.

  1. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    Hi,

    I am just curious if anyone has any tips on how I could achieve some "depth" in my 2.5D game. I am working on a game with a viewpoint like the classic Zelda game for NES, or if something along the lines of "The Escapists". By that I mean I have a top-down camera angle, but the assets are shown sideways in such a way that they are supposed to be able to walk along both the X and Y-axis.

    My player object(s) consists of many separated body parts (head, chest, overarm, underarm, hand, etc) that are changeable based on equipment/gear. I have set each body part to a specific layer so that they will appear in the right order. This works nicely. Problems occur when they are trying to move behind or in front of trees, rocks as well as other players. So far I have kept all Z values to 0, but I need a way to tell my player(s) if they are in front of or behind items they collide with so that I can render them in the right order. My best guess is to change the Z value equal to the Y value so that when they walk downward on the screen, they will also move closer to the camera (on the invisible z-axis). Has anyone done something similar that might have a good idea on how to solve this, I couldn't even figure out a good way to just set my Z value directly.

    PS: I would prefer to keep creating my animations in Unity.

    Thanks in advance,
    Tom
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    If these are sprites and the body parts are different sprite renderers, then there's the Sorting Group component which allows you sort child objects based on a single position - the parent object which has the Sorting Group. This lets you avoid the nuisance of adjusting z/y values.

    https://docs.unity3d.com/Manual/class-SortingGroup.html
     
    thooom likes this.
  3. Zelacks

    Zelacks

    Joined:
    Jul 23, 2019
    Posts:
    2
    You could even try a hybrid approach, with the link lo-renzo posted (https://docs.unity3d.com/Manual/class-SortingGroup.html), you could set up your equipment/body part rendering using sub-sorting groups so that all the sprites on the character work out as expected, no need to mess with the z depth.

    But then on the highest level SortingGroup for your character(s), set the Order in Layer to 0 (meaning that all the subrenders for that character either completely render infront or behind other characters based on their z pos) and modify the z position based on the y position.

    This would let your character(s) render based on whichever is in front of each other, but would ignore complexities of trying to manage the z pos of your character's sub renderers (items/body parts etc).

    And if you want other object to always render infront, you just need to set the objects highest level SortingGroup to be 1 (or greater).
     
    thooom likes this.
  4. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    Thank you both, I will look into this tonight. I'll post here on my progress for anyone trying to figure out the same thing in the future.
     
  5. thooom

    thooom

    Joined:
    Apr 29, 2014
    Posts:
    11
    This seems to work nicely btw, for any future people that have this issue.