Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tree layer issue

Discussion in '2D' started by dccoo, Aug 23, 2016.

  1. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    Hey guys, since I was into 2D games I have this doubt...
    Suppose I have a tree and a player (in the perspective of rpgs like chrono trigger or so)

    tree1.png

    I was never able to discover how to make the player appears in front of the tree and vice-versa.
    For me, it look like a 3d issue and I thought of some solutions but not very good, for example:

    Divide the tree in 2 parts, the bottom one would be a layer behind the player and the top one, in front of the player, with the collisor between the 2 parts.
    tree2.png

    but this seems fake and sometimes is just bad, like in the third case, when the player collider hits the tree collider when it shouldn't... so my question is:

    How can I do this effect correctly?
     
  2. Pagi

    Pagi

    Joined:
    Dec 18, 2015
    Posts:
    132
    Put this into Update of your player and in Start of your tree.
    Code (CSharp):
    1. transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.y / 1000);
    Higher Z value means further in background.Both object must have same Sorting layer and Order in layer for this to work.

    EDIT: Also if you want your player and tree to collide, I recommend putting circle collider around the root of tree, not any higher.
     
    theANMATOR2b likes this.
  3. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    omg i freaking love you thanks <3