Search Unity

  1. Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice

Tilemap - Pokemon perspective, how to define layer from y position

Discussion in '2D' started by danBourquin, Jan 11, 2018.

  1. danBourquin

    danBourquin

    Joined:
    Nov 16, 2016
    Posts:
    17
    Hello everyone!
    I'm trying to make a 2d game with the same perspective as Pokemon. I use the tilemap object from unity 2017.3.0f3

    My problem is I'll move a character inside this map and I'll need that he can go behind the car or in front of it depending of his y position.

    I already know that I can make different layer but it'll be complicate to make one layer per tile line... So is a way to use the tilemap object on my case or do I need to make or buy another tilemap plugin ?

    Thank you for your help!

    Dan
     
  2. Joimer

    Joimer

    Joined:
    Aug 1, 2017
    Posts:
    14
    Well, an easy way is not to make the car part of the tilemap but an external sprite that you can place on top of the tileset. Classic games would just make the whole car a full collision (remember the truck in RBY), in your case you're gonna need a script that checks the position of the car relative to the player to decide its order in layer. If it's a gameobject with a sprite renderer, you can easily attach a script that checks the y position of the player each update to change the order in layer.
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,090
    We used this to help us, best resource out there:

    https://breadcrumbsinteractive.com/two-unity-tricks-isometric-games/

    Has working code.

    EDIT:

    This is what you need for the basics:

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. [RequireComponent(typeof(Renderer))]
    3. public class DepthSortByY : MonoBehaviour
    4. {
    5.  
    6.     private const int IsometricRangePerYUnit = 100;
    7.  
    8.     void Update()
    9.     {
    10.         Renderer renderer = GetComponent();
    11.         renderer.sortingOrder = -(int)(transform.position.y * IsometricRangePerYUnit);
    12.     }
    13. }
    EDIT2: That will auto sort everything based on Y pos, no need to define layers per object
     
    Joimer likes this.
  4. danBourquin

    danBourquin

    Joined:
    Nov 16, 2016
    Posts:
    17
    Thank you for your help!

    It'll work fine if I apply this solution to the character and the car. But for static map object. Like the big lamp on the street ? My question is can we make change the order of layer of a specific tile in a tilemap.
    Otherwise yes I'll do my own tile map tools and handle the sortingOrder depending of the y axis!
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,767
    Use the Custom Transparency Sort Axis. This will make unity automatically default to sorting by the axis you define instead of the camera vector, or depth. No scripts necessary.

    https://unity3d.com/learn/tutorials...ion/sorting-groups-and-transparency-sort-axis

    https://docs.unity3d.com/ScriptReference/TransparencySortMode.CustomAxis.html

    Edit > Project Settings > Graphics

    Objects will sort based on the location of their pivot point. For a grid based game, the center point should be fine. For more granular positioning, putting pivots at the place where the sprite meets the ground is a good way to get proper sorting.

    For objects made of multiple parts, you can add a SortingGroup component to the parent object to have that group of objects sort as if it were one object.
     
    Last edited: Jan 11, 2018
    Joimer and danBourquin like this.
  6. danBourquin

    danBourquin

    Joined:
    Nov 16, 2016
    Posts:
    17
    Thank you very much it's exactly what I was looking for!
     
    LiterallyJeff likes this.
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    654
    Are you positive it sorts around the pivot point? In unity's official tutorials on this they say it sorts from the center of the sprite. And that is the behaviour I am getting, which is the reason I switched to using a script that changes sort order instead.

    Also the issue with sorting group is that you usually don't have a sprite renderer on the same object. So there is no pivot on that. Just some issues I ran into.
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,767
    After some testing it appears that sprite pivot is not considered with sorting axis, but I still wouldn't suggest the scripted sorting approach.

    Using the sorting axis is the easiest and most efficient way to do sorting, so I recommend using that and trying to work within its limits. (sorting can still always be overridden by manually setting sorting order/layer)

    In a grid based game I would think center point would work for the majority of cases, but one way to work around that is to give your sprite a parent object, and give that object a sorting group. The sorting group's transform will act as the pivot for that hierarchy, and you can offset your child sprite however you wish.
     
  9. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    654
    I like the idea of using a parent object w a sorting group together with the sorting axis. Think I'll give that a try :) Really wish they would make pivot work together with the sorting axis but oh well.
     
    LiterallyJeff likes this.
  10. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    654
    Curious if you have thought of a solution for something like a large L-shaped building. If we sort from a bottom pivot here it would look weird when the player approached one of the "wings" on the side that are placed higher on the Y-axis, but Unity does not know that since it's all one image. Can't see a solution for that using sorting axis except seperating the building into several Objects?

     
    Last edited: Feb 11, 2018
  11. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,767
    Yeah depending on where the character is allowed to move/overlap with the building, you can either move the pivot somewhere that works in all cases, or break it up into multiple objects sorted individually.

    For a case like this, where there are multiple "depth" values so to speak for one object, I think breaking it up into two renderers is the cleanest solution probably. If possible, try to build your objects in the mindset of modular tiles, to make this kind of sorting as easy as possible. Sometimes that's not easy with a more organic art style though, but slicing and re-assembling isn't that bad of a workflow.
     
    Last edited: Feb 12, 2018
    mrCharli3 likes this.
unityunity