Unity layers not working as I want - c#

So I'm very new to Unity and creating my first 2D game, It will have a player, monsters, platforms and a static background image.
So What I've done is set a 2D sprite as a background image which is on the default layer. I also got a character from the asset store that I just pulled in to the game and set to User layer 8 (player).
The platforms randomly spawn on the map and was not visible through the background at first but when I set the sprite sortingorder to 1 they were visible.
Now for the monsters. They are also from the asset store but inserted into the game via C# code. If I have a the background on screen they are still not visible even if i set the GameObject.layer = 8 for them(to same layer as player). Why? What Is the problem here.
Thanks in advance.

Unity has two types of layers, the ones you're changing are not the ones that determine drawing order, you need to change the SortingLayer and OrderInLayer parameters in the SpriteRenderer component.
https://unity3d.com/learn/tutorials/topics/2d-game-creation/sorting-layers
If you're only using sprites then you can keep all of them in the same SortingLayer and change the OrderInLayer of the background to something like -100.
If you're using 3d models, youre gonna need to manually set the positions of the objects closer or away from the camera (and keep all the sprites on the default SortingLayer).

Related

Why is Unity Canvas Image always rendering over my gameObjects even though it is definitely behind them?

In my 2d Unity project, I have a Canvas with an Image that I want for a Background.
I have 2 gameObjects in front of this background. But no matter how much fiddling I do with Pos Z, Sorting Layers, or hierarchy sorting, the image is always in front of the objects.
Gif above shows in 3d mode that even though the image is clearly behind these objects, it will always appear over them if they overlap.
Hierarchy:
Main
Camera (Inspector: https://i.imgur.com/Q5a52cf.png)
BackgroundCanvas (Inspector: https://i.imgur.com/m9Pxr6B.png)
BackgroundImage (Inspector: https://i.imgur.com/jTx7pEW.png)
Object1 (Inspector: https://i.imgur.com/YcClEhk.png)
Object2
Any advice to rescue me from this madness is much appreciated.
Set the sprite renderer's transform z value to 0 instead of 100
If that does not solve, please specify camera properties also, so I can try to recreate the exact setup.
Try clicking on Layers -> edit layers, inside sorting layers you can change the order grabbing layer, everything upper appears behind in the camera.
You could create a layer called Object
Assign it to the game objects.
Create an object camera
culling mask -> object layer
depth bigger than you current main camera.
Set it to Projection -> Orthographic
Clear flags -> solid colors.
canvas Render Mode -> Screen Space - Camera and assign the Render Camera to be the Object Camera
Inspector tab of the object or background.
Sprite Renderer.
Additional Settings.
Sorting Layer.
change it to a different layer.
Had this same issue and was able to fix it with these steps:
In canvas settings change Screen Space Overlay to Screen Space Camera
Set the camera variable to the one you are using for your scene.
I figured out a workaround. I basically created a VisualElement inside the UI Builder and set a render texture to the background. Then I added an extra camera to my project to view all the sprites that needed to be on top. That camera feeds the render texture, so now everything that camera sees is forced to be on top of the UI Document as the background of that VisualElement. If you want control over the whole screen, just set the VisualElement position to absolute and max out its dimensions. If your game doesn't have a fixed aspect ratio it might cause some stretching, but other than that I cant really tell the difference. Sorting layers for the UI Documents are broken and unity needs to work on that. This is the best option I've found. Hope this helps.
I had the same problem and I fixed it by attaching the camera to the canvas which is screen space and finally changing the sorting layer of my object to -1.

Unity Game View Line

I'm learning Unity but when I create a map with Tile Map and look at the map in the game view it has some lines
It's tilemap aliasing, usually goes away when you try a standard resolution in the gameview, which you already have but try 1x scale, also try something like 720p, 1080p or an actual monitor resolution and make sure the game view is set to "Fixed resolution". If the aliasing disappears, you will know it only appears at weird resolutions and you won't have to worry about it.
If it doesn't, theses are some of the things you can try:
First make sure that the filtering mode for your tilemap sprite/s is set to point.
Add a new material to the tilemap gameobject and set it's shader to Sprites/Default, then check the "pixel snap" box.
Try the pixel perfect camera https://blog.unity.com/technology/pixel-perfect-2d
Set the grid cell gap on x and y axis to a very small negative number, like -0.0001
If all of these doesn't work, I think you will have to use the sprite packer to pack your tilemap into a sprite atlas and make it so that there are gaps between each tile. If you wanna know why that is: https://forum.unity.com/threads/tilemap-has-tearing-between-tiles-even-with-pixel-snap.499154/#post-3292216

Set a GameObject in front of an image

I'm having trouble setting an image behind a gameobject in unity. I saw a lot of similar questions but none solved my problem. So basically I need to have the image as a child of the object to be able to do what I'm to do. I added an image of my unity view.
You can likely cheat this by using multiple cameras and layers. Set your container GameObject to its one layer (say "GameObjectLayer",) and set your image's GameObject to another layer (say "ImageUILayer".)
Then, create two cameras, and set the following properties:
Camera 1:
Depth = 0
Culling Mask = "GameObjectLayer"
Clear Flags & Background = (whatever you want the main camera background to be)
Camera 2:
Depth = 1
Culling Mask = "ImageUILayer"
Clear Flags = Depth only (this keeps the camera from drawing a background at all)
Each camera renders in turn, starting with the lowest depth and working up from there. Thus, Camera 1 renders everything in the "GameObjectLayer" first; then, Camera 2 renders everything in the "ImageUILayer" over top of that.
It's an extremely useful workaround when you're trying to mix 2D sprites with 3D objects, and it will never give you any flicker or other z-positioning weirdness, because the cameras each render all their layers in turn.
You can use more than two cameras if you need more than two layers of 3D/2D objects; I'm currently using a total of seven cameras with no ill effects.
in your case I suggest you use the(right click and choose)>2D > Sprites and put the sprites behind of the Gameobject

Spawn prefab in front of UI Image

I spawn the prefab in a gameobject and that gameobject is a child of UI Image what I'm trying to accomplish is display the prefab infront of a UI Image. I've tried making z position of gameobject to negative but nothing happens. I can't place the sorting layer of UI Image cause it doesn't exist. What should I do? Is this even possible?
Update
Tried adding second camera. I made gameobject layer to SecondCam. Change settings of second camera to depth only and it's culling mask to SecondCam only then it's layer to SecondCam also. Change settings of main camera culling mask to everything except SecondCam. But this doesn't work, Have I done something wrong?
Second Camera settings
Main Camera settings
If your Gameobject is not one of the UI components you may need to give it another layer and render it with another camera with depth property higher than the UI camera.
You can change the sorting layer on prefabs or any object created on runtime.
If your UI canvas is set to screen overlay, then it with ALWAYS render on top. You have to use a screen space or world space UI, along with Dave's answer.

Unity2D Gameobjects covered by canvas

I am having a problem with my unity canvas as my background is on top and covering my gameobjects. I have tried putting the canvas sorting order to -10000 and changing the canvas rendering mode to overlay/camera/worldspace, but none of them works..
This is how my game looks like right now:
as you can see the white image which is my background is covering my gameobjects..
Attached are my component values for my canvas:
my canvas
Attached are the values for my camera:
my camera
Attached are the component values for my gameobjects:
my hero gameobject
Have you tried setting your game objects z position to a negative value?
For 2D games in Unity, the more negative your Z value is, the "closer" it is to the camera. If you try setting your knight to something like -1, it should no longer be obscured by your background.
Also, make sure to set the camera for your canvas' render camera and you can set the sorting order back to whatever it was before. Just check the individual pieces attached to your canvas to make sure their Z positions aren't "in front" of the game objects.
The background for your game shouldn't be on the canvas, it should be a gameobject with a sprite renderer and need to be attached with a sprite(background image) with a resolution that will fit most mobile screens (assuming you build it for mobile phone, i personally prefer making it 1028X720)
if you still insist on putting it on the canvas then you could set the canvas render mode to world space and set the sorting layer to something less than default or sort order -1
I know it is old post but #user8502296's answer saved me so I wanted to contribute. I changed the Canvas->RenderMode->Screen Space-Camera then I added Render Canvas->Camera->Main Camera.
Canvas

Categories

Resources