Hello, so I’m new to programming / Unity and am currently making a 2d dungeon crawler. I am trying to make a button to “Speak to Old Man” and have it not move with my game (I.E make the button always appear directly under the Old Man), but have been having difficulties. I originally tried having the button and dialogue box share a canvas, but couldn’t figure out how to make the button stay directly underneath the old man. I then tried creating a second canvas with render mode World Space and placing the button in there but it then becomes invisible no matter the sorting layer I set it to.
Current example with 1 canvas:
link:
[1]: Screen capture - 694011d0b2076730d492ebaf9d6fe96d - Gyazo
You actually do want to use a worldspace canvas for that, they are built exactly for the kind of situations you are dealing with, a moving camera while the canvas should stay on a fixed worldposition.
Using an overlay canvas or a screen space canvas will keep the canvas on the same position on the screen, but with a moving camera that is not what you want here.
Your problem seems to be that your canvas is invisible when set to worldspace. There are multiple ways to fix this:
- First of all, imagine your worldspace canvas like an actual object within your scene, which thus can also be obstructed by anything infront of it. So check that the z position of your canvas is closer to the camera than the z position of all other objects on your screen.
- Depending on your Render Pipeline you can also create “global sorting rules” for rendering. Within the URP (Universal Render Pipeline) for instance you can create “Render Features” within your ForwardRenderer asset, in which you could define that all objects on the layer “UI” should always render AFTER all other opaque objects have rendered (or after post processing… whatever you want). Using this approach it doesn’t matter anymore what Z position your canvas has, it will be always rendered infront of all other stuff.
You can look into the concept of Overlay Camera.