Search Unity

Sorting sprites from different object layers within the same sorting layers

Discussion in '2D' started by Klawiszq, Nov 8, 2019.

  1. Klawiszq

    Klawiszq

    Joined:
    Oct 28, 2019
    Posts:
    2
    I've got a troublesome situation in which i need to be able to sort sprites from different object layers within the same sorting layer, I've got a mesh that is working as my field of view raycast which requires certain objects to be on different layer in order for it to work, however doing that excludes theese object from my sorting script, which looks for Y position (or at least they are being sorted on Mask layer instead of Object layer).

    I'd like to keep the masking functionality while, being still being able to sort the sprites within the same sorting layer, is it possible, if not what are the other options to solve this problem.

    Sorting Script:
    Code (CSharp):
    1. [ExecuteAlways]
    2. public class PositionRenrederSorter : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     private int sortingOrderBase = 0;
    6.     [SerializeField]
    7.     private int offset = 0;
    8.     [SerializeField]
    9.     private bool runOnlyOnce = false;
    10.  
    11.     private Renderer myRenderer;
    12.  
    13.     private void Awake()
    14.     {
    15.         myRenderer = gameObject.GetComponent<Renderer>();
    16.     }
    17.  
    18.     private void LateUpdate()
    19.     {
    20.         myRenderer.sortingOrder = (int)(sortingOrderBase - transform.position.y * 50 + offset);
    21.         if (runOnlyOnce)
    22.         {
    23.             Destroy(this);
    24.         }
    25.     }
    26. }