Search Unity

2D Sprite always behind background

Discussion in '2D' started by Ryxn, Aug 2, 2016.

Thread Status:
Not open for further replies.
  1. Ryxn

    Ryxn

    Joined:
    Aug 2, 2016
    Posts:
    4
    I am creating an 2D android game. I got a helicopter sprite and added as a 2D sprite however, it always goes behind my background for some reason. Here is a screenshot:

    why is this happening?
     
  2. Green-Sauce-Games

    Green-Sauce-Games

    Joined:
    Mar 27, 2014
    Posts:
    71
    When using Sprites, you must change the Z Position to change who will appear in front of who.

    If you want do sort by hierarchy, you must use Image and not Sprite (but Image is better for UI, so use the Z order) =).
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Ideally Z ordering is the last thing to determine what is in front of what. Typically in a 2D game you almost never have to worry about Z if you do things right.

    You have order in hierarchy for the canvas, as well as Sorting Layer and Sorting Order on every Sprite Renderer and Canvas.

    Try creating more sorting layers Background, Midground, Foreground, etc, and assigning the objects to the correct layers. Sorting Order is used to determine order within a layer.

    https://unity3d.com/learn/tutorials/topics/2d-game-creation/sorting-layers

    Edited (2018): Now you also have SortingGroup, which makes everything underneath it in the hierarchy sort locally, and the entire SortingGroup is then sorted together preserving that local sorting among other Sprites and SortingGroups.

    Alternatively, you can use the TransparencySortMode in the settings to set a custom axis for sorting, which for instance could be set to sort on the Y axis. See this for an explanation of that technique:
    https://unity3d.com/learn/tutorials...ion/sorting-groups-and-transparency-sort-axis
     
    Last edited: Apr 8, 2019
  4. Ryxn

    Ryxn

    Joined:
    Aug 2, 2016
    Posts:
    4
    I created these layers:

    I assigned each object to a layer:


    however I am still getting the same results. Am I doing it right?
     
    oivanekaio likes this.
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Yes, if the background is a UI Image and still in front of the helicopter, check your Canvas settings for Sorting Layer and Sorting Order.

    Also, you don't have to keep Layers and Sorting Layers identical. For render-order, Sorting Layer & Order is what matters. The other Layers is for grouping objects for things like Collision interactions, raycasts, use with LayerMasks, etc.
     
    Last edited: Aug 2, 2016
  6. Ryxn

    Ryxn

    Joined:
    Aug 2, 2016
    Posts:
    4
    I set the Render Mode on the canvas to World Space and it seems to be working now. Thanks :)
     
    Quark1010, oivanekaio and ruslancik like this.
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    No problem. One thing to keep in mind is that Screen Space - Overlay will render above everything (probably what you were seeing). Camera Space has Sorting order and renders relative to the camera bounds. World space canvas exists in the 3D world relative to the canvas in world coordinates.
     
  8. sayan0342

    sayan0342

    Joined:
    May 28, 2017
    Posts:
    1
    if you still facing this problem then in the transform pane into the position pane put any negative value of z then your selected sprite will appear in front of your background

    viz i have selected z = -0.01 and it will really work
     
  9. stealthgamer2016

    stealthgamer2016

    Joined:
    Mar 10, 2018
    Posts:
    1
    i love u
     
    oivanekaio and bcad2130 like this.
  10. MBrown42

    MBrown42

    Joined:
    Jan 23, 2018
    Posts:
    86
    Hello, glad this got fixed. I also had issues with my 2D game between sprites and backgrounds that were on meshes. If it helps anyone who lands on this thread, here is a tiny script I wrote that you can attach to any gameobject that has a mesh renderer - it lets you easily set the sorting order which will line up with Sprite sorting orders. Note it only uses the Default layer, but you can easily change that by making a public string of the layer and putting it in place of "default", then setting it in the inspector.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MeshRendererSortOrder : MonoBehaviour {

    public int sortOrder;

    // Use this for initialization
    void Start () {
    MeshRenderer mr = GetComponent<MeshRenderer> ();
    mr.sortingLayerName = "Default";
    mr.sortingOrder = sortOrder;
    }


    }
     
    oivanekaio likes this.
  11. roland-_-

    roland-_-

    Joined:
    Apr 6, 2020
    Posts:
    1
    This worked! Thanks
     
    vividglows and oivanekaio like this.
  12. UDN_0a6458fc-da4c-42fa-83e0-9bd9f3729f7d

    UDN_0a6458fc-da4c-42fa-83e0-9bd9f3729f7d

    Joined:
    Apr 14, 2017
    Posts:
    1
  13. burakekincioglu

    burakekincioglu

    Joined:
    May 8, 2020
    Posts:
    3
    Hey, it is also about your Canvas sequence: I have spent much time to solve it, unfortunately :) . I mean; If you add the dark green image to your hierarchy canvas before light green image, the dark green image will always be behind of light green, and vice versa canvas.JPG
     
  14. Frazer88

    Frazer88

    Joined:
    Feb 14, 2021
    Posts:
    2
    I fixed this by changing the 'Sorting Layer' property on the 'Sprite Renderer'.
     
  15. Deleted User

    Deleted User

    Guest

    nope. this doesn't work unless you 've left out alot of other issues
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Let's close this thread, it's just being necro'd without anything useful being added.

    If you have an issue, create your own thread, state what you've tried etc. Just posting a single-liner on an old thread is lazy posting and won't get your problem solved.

    Thanks.
     
Thread Status:
Not open for further replies.