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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Layering issue, not sure how to go about this

Discussion in '2D' started by DragonFhangDev, May 29, 2021.

  1. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Hey guys, So I have a layering issue I want to script up, I'm basically looking for help on how to script it since I'm still learning a lot about coding so I just need suggestions and info on how to do it please.

    I've included some pictures of my player and a road sign object. I basically want to be able to have my player close to the object instead of his full body away. The players collider is a circle which starts around the middle of his chest and ends a little under his feet.

    On any scene of my game I have objects that of course will appear in front of my character as well as behind the character. I am trying to figure out how to go about it so my player's body doesn't glitch through the object when he's not behind it. What's the best course of action to take? I'm going to assume I need to change the sorting layer and add in some triggers. I'm just stuck on where to start.





    Any help would be awesome, thanks!
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    use transparency sort axis
     
  3. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    I already have that enabled as Custom Axis and set to 1 in the Y axis.
     
  4. Rickxn

    Rickxn

    Joined:
    Mar 21, 2021
    Posts:
    9
    Some tips can be found by googling, eg: https://www.reddit.com/r/Unity2D/comments/96dqku/how_to_make_player_go_in_front_and_behind_objects/

    So it seems you need a script attached to the object (sign) that changes the render order depending on the y position of the player. No code, because im a noob too, but you can figure something out: "if the other GameObjects (player) y position is lower than the objects (sign) y position (maybe +some additional space), then change sorting layer order of object (sign)".
    Player and sign need to be on the same layer. Inside the layer you can change render order with this:
    "gameObject.GetComponent<SpriteRenderer>().sortingOrder -= 1;"
     
  5. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    I got that all sorted out thanks guys.
     
  6. Rickxn

    Rickxn

    Joined:
    Mar 21, 2021
    Posts:
    9
    Please share your solution for others, who might have the same problem. :)
     
  7. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    I just mistakenly put the wrong stuff in my transparency sort axis. I didn't pull full enough attention lol.