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
Related
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.
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.
I have two cameras,
why I have two cameras?
because I want some game object in front of the Canvas so that it could move around, maybe could have some particles if it feels happy etc.. So:
I set up the main Camera with mask option UI and make it in charge of all the UI stuff, and to make sure it does not overlap everything I set the canvas mode "Screen-Space Camera"
I set up another camera and make it capture everything else except the UI stuff.
set their depth so that the main Camera with UI will behind the other Camera.
things go well now, I could see the game object before the UI stuff. Cheers!
But in some case I what to convert the UI element to the point in the world space, So that I could generate some gameObject near the UI element and maybe Tween it to move to another UI Element, let's take collecting Gems as an example.
Usually, I could just do that with following code:
GameObject go = Instantiate(eff_gem);
go.transform.position = Camera.main.ScreenToWorldPoint(p1.GetComponent<RectTransform>().position);
go.transform.DoMove(p2.GetComponent<RectTransform>().position, 1f).OnComplete(()=>{
Destory(go, 1f);
});
The main idea is to instantiate a gem near a button and make it fly to the count panel.
But since I make the canvas "Screen Space Camera" instead of "Screen Space Overlay", all the above code will be a mess. furthermore, I think I have to convert the screen point of the canvas element to the world space by the other camera, But I can't even get the actual pixel position of the canvas element by
p1.GetComponent<RectTransform>().position now.
So How Can I do that?
I have made a demo project to demonstrate the issue.
var p1R = p1.GetComponent<RectTransform>();
Vector3 p1WorldPos = p1R.TransformPoint(p1R.rect.center);
//use p1WorldPos in your other camera
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
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).