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?
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) =).
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.
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:
I created these layers:
I assigned each object to a layer:
however I am still getting the same results. Am I doing it right?
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.
I set the Render Mode on the canvas to World Space and it seems to be working now. Thanks
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.
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
i love u
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 ();
mr.sortingLayerName = âDefaultâ;
mr.sortingOrder = sortOrder;
}
}
This worked! Thanks
Sorting layers really
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
I fixed this by changing the âSorting Layerâ property on the âSprite Rendererâ.
nope. this doesnât work unless you 've left out alot of other issues
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.