Search Unity

Snap an object to another object and set the layer order right.

Discussion in '2D' started by seventythreeers, Aug 21, 2017.

  1. seventythreeers

    seventythreeers

    Joined:
    Nov 15, 2016
    Posts:
    18
    temp.png
    I am having this problem and hopefully you can give me some advice. I have a chair object and a character object in a 2D scene, both objects are draggable can be placed anywhere. Right now the layer order is determined by their y positions with this script:

    void Update () {
    GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1;
    }

    So the objects closer to the bottoms are always in front of objects above them in terms of layer order. But for special cases like this chair and the character, if the character is dragged close to the chair, the character should be in front of the chair even its y position is above the chair's y position, or if the chair is dragged close to the character, the chair should be behind the character automatically.

    Can anyone give me some advice how can this be done? Thanks in advance!!!
     
  2. Deleted User

    Deleted User

    Guest

    You can use their Z position to order objects on the same sorting layer, make sure your camera can see all the Z values you're using and put lower values on the things you want in front.