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. Dismiss Notice

2D Pixel Top Down - Above and Below Bridge?

Discussion in '2D' started by nproject, Mar 31, 2016.

  1. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
    Hello guys,

    I want to create 2D Pixel Top Down Game and I have some question about when a character above and below bridge. What is the best way to achieve this result ?
    Right now to solve this, I'm using 2d box collider to detect when player coming from above. Because I'm thinking about AI/Enemy when they walk there and also I using Poly Nav from assetstore (https://www.assetstore.unity3d.com/en/#!/content/14718) define the walkable and obstacle area for AI.

    See pic below.

    Thanks
    PS, pic from Faraway Game (Not my game), just to help me explain the question

    jisMd5m.png
     
  2. foxifi

    foxifi

    Joined:
    Dec 20, 2015
    Posts:
    55
    I think there are quite a few ways to handle this.

    I'm starting to try to work it out myself, but for my game I'm basically forced to use 3D physics at the moment, so I will be using the Z axis some anyway.

    My game art is similar to the image you posted, whereally "height" is expressed sort of isometrically extending along the Y axis. Currently, I have a script on all objects that sets the sorting layer of the sprite renderer based off the Y axis, so anything higher in Y will be rendered on top.

    I think the next step, for me as well as a possible solution for you, would be to first sort by Z, then Y, so that I can make the first "level" of height at Z =5 or 10 or whatever, while the ground remains at 0. Walking up stairs and things to get the player up to the next level would be a trigger that moves the player up inow the Z axis to the desired level.

    If the sorting is working, this would solve your problem without any additional colliders or logic.
     
  3. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
    Hi @foxifi

    I'm also using sorting layer based Y axis to solve problem with front and behind of building or something.
    But when in this case you use the same sorting layer based Y axis, you will get the same Y value when above or below bridge. so the result will be the same. :(
     
  4. foxifi

    foxifi

    Joined:
    Dec 20, 2015
    Posts:
    55
    Yeah, this is true. I was actually trying to think of a simple way to include the Z axis, as I will be using it. If you don't want to, I'm sure there is another way, but I don't see any downside to placing your top tiles (those on the same level as the bridge) on a higher Z axis.

    If you do this, you can easily see what tiles are higher up.

    I think then you could apply the Z axis to the sorting.

    Instead of only sorting by the Y axis, it may be possible to weight these values and end up with a number that is representative of that new sorting.

    Like, Z * (0.6) + Y * (0.4).

    This would work like a weighted average, where things on the same Y value but higher Z axis would be rendered first.

    I haven't put enough thought into this to see the caveats but it seems like a simple way to handle levels of height.
     
  5. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
    Maybe can you give me script to do that and how?

    Let me know if you solve this.

    Thanks
     
  6. foxifi

    foxifi

    Joined:
    Dec 20, 2015
    Posts:
    55
    I'm not at my PC and I'm just a hobbyist, but I'll be working on solving this very soon. I will share what I find with you if you haven't found a better solution, apologies if this doesn't help.
     
  7. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
    Haha don't worry about that i'm in no rush and thanks for helping me ;)
     
  8. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    Could you not just have an elevation value that adds/subtracts from the Y axis value? e.g. order in layer = Y axis +/- elevation. Then whenever the player moves up/down elevation you just update the that elevation value for him. So if player is on elevation level 0, and the bridge is on elevation level 1. The player will move underneath it. But when the player climbs up to elevation level 1 he will now be above the bridge, regardless of the Y axis sorting.

    Haven't tested this, but it seems logical.
     
  9. foxifi

    foxifi

    Joined:
    Dec 20, 2015
    Posts:
    55
    @Isaiah Kelly

    Not OP but I think this is the same type thing I was suggesting, just using Z as the elevation. The problem with just adding or subtracting to the Y value is that the sum can be the same if the two values are inverted, which means if something is tall enough, it will mess up the sorting layer.

    The other issue to resolve is that the player object and any other objects need to follow these rules but also be above these other things, as well as sort with themselves.

    Hopefully soon I'll have some free time to work on this and really test it out, and possibly find the answer.
     
  10. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
    I have solve the problem.
    Here what I do right now
    - I'm using boxcollider2d.
    - when player enter bridge, AddSortingOrder = bridge.sortingOrder, I will increase player sortorder and make player above bridge.
    - when player out of bridge, AddSortingOrder = 0, So player will behave like normal (below bridge)
    Code (CSharp):
    1.  
    2.  
    3. if(player.isAbove == false){
    4. AddSortingOrder = bridge.sortingOrder;
    5. player.sortingOrder = AddSortingOrder + player.sortingOrder;
    6. player.isAbove = false;
    7. }
    8. else{
    9. AddSortingOrder = 0;
    10. player.isAbove = true;
    11. }
    12.  
    I have test this also make really long bridge, they seem fine and no issue.

    why I use sortorder instead of Z axis ?
    because
    1. I just know that you cannot use Z axis and sort order at the same time. Even Z is closer to camera, but sortorder is lower. Player won't appear in the camera, but in 3D view playerobject is in front of bridge.
    2. I have a script attach to every object always change the sortingorder based on object Y axis, so I can create player in front of and behind of building. And if I use Z axis, I'm afraid player Z axis will move behind maincamera.

    If you guys find better/ more simple solution, let me know. I'm welcome to try it ;)
     
    dox187 and ariyalion like this.
  11. foxifi

    foxifi

    Joined:
    Dec 20, 2015
    Posts:
    55
    Hey! I'm sorry but I don't think I clearly explained what I meant by using the Z axis.

    I don't mean to sort your objects using the Z axis, literally. What I meant was setting the sort order to consider more than just the Y axis. Currently you are setting the sort order based on the Y axis, but if you include some other value, it may be possible to sort by the Y axis, on a given level of height. I just was using the Z axis as an option because it would be easy to get an objects height level from its transform in code without having to get component.
     
  12. gregoired

    gregoired

    Joined:
    Apr 8, 2013
    Posts:
    20
  13. nproject

    nproject

    Joined:
    Jan 16, 2015
    Posts:
    68
  14. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    The way that I solved this in my project is a combination of sorting layers and collision layers. Each cliff "level" has its own sorting layer, and in between these layers is an alternating collision layer. As in:

    1. at level 0, collision layer A
    2. at level 1, collision layer B
    3. at level 2, collision layer A
    4. etc.

    This way, the character on the bridge will never collide with the character on the ground, and the character on the bridge will always look to be "above" the character on the ground.

    You will have to figure out the best way of assigning collision layers based on the character's location. I, for instance, have trigger collider "switches" that switch the character between collision layers A and B and levels/sorting layers 0 and 1, which are placed around the entrance to the cliffs.
     
  15. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    Yep I was thinking Sorting Layers and Order in Layer.

    One Sorting Layer per level e.g. ground , stairs to floor one , floor 1, stairs to floor 2 etc.

    With each of those layers the sprites each have Order in layer set to decide who is in front on that layer.

    So if I am on the stairs to floor one layer I would make my sprite go to the top of the stairs. When I reach the top I would change to the sorting layer floor 1. The reverse going down, stairs to floor one layer then finally ground layer.

    In my game I have Background , Foreground, Trees as Layers and on each layer have my sprites order within that layer as the game demands. So same as top down except I look straight ahead.

    So a background on the Floor 1 Layer might be the bridge and its order in layer may be 1. You may allocate 5 order in layers to background on level 1. Then you may have moving objects like your character who runs around on floor 1. His layer is also floor one but his order in layer is say 6. This ensures he appears in front of all backgrounds in that layer. You may allocate 6-10 order in layers on floor 1 to be where moving characters are located. Foreground images on floor 1 like maybe an umbrella at the beach may be allocate sorting orders 15-20 so they appear in front but aren't another layer.

    Lets say your character wears a badge or gun. Using the example above your character would be on order in layer 6 on layer Floor one and the characters badge would be a sprite of your characters game object with an order in layer set to 7 on floor one layer so its in front.

    So in essence each level has a Sorting Layer.
    Each sorting layer has order in layer.
    Each sprite at one time has a sorting layer and a order in layer.

    Each sorting layer (Eg ground level, stairs to floor one, floor one.) you work out what types of objects apply in that, e.g. background, characters, things above the characters but on the same level. You work out a range for each one. Eg. background on a level(sorting layer) uses "order in layer" 0-5 for sprites. Player characters and enemies can use 6 - 20 for their moving sprites etc, on the same sorting layer, things above your character are say 21-25 order in layer for them.

    Come up with what your game needs as far as a sorting goes and set it in stone for what a level (sorting layer) is as far as order in layers go. Then just change sorting layers as needed and because all your order in layer settings are the same for every level you only ever have to change the sorting layer dynamically..

    If an enemy fires a projectile up a level you change the projectiles sorting layer appropriately when it reaches your characters level.

    Also work out where players can go up and down layers and in between layers(staircase layers) in the game so you know when to change the layers appropriately. You could use a stationary game object with no sprite with a trigger collider to detect objects coming onto or off stairs and dynamically change the passing objects sorting layer as they go by.


    1. So create the Sorting layers that you need in the editor.
    2. Work out what groups of objects you need on a layer. Background, Characters, Things Above Characters etc.
    3. Allocate those groups of objects "Order in Layer" ranges. The higher are the ones that appear in front.

    Eg. Background sprites say are all in 0 - 10, characters and enemies in say 11-20, Objects above your characters 21 - 30.

    Once you sort your order in layer ranges out. They will never change on each sorting layer you move too.

    All that changes is the Sorting Layer.



    I hope that sort of helps.
     
    Last edited: Apr 3, 2016
  16. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    199
    Great explanation