- Home /
GUI Button overlay separate from main camera
Hi, I am working on a 3D game on android. My gamescene has a main camera pointing at about 45 degree rotation in x axis.
Now I would like to put in some touch button to my game. However I realize that the touch button does not display correctly due to the main camera view angle. How can I separate the touch buttons and other guiText from the main camera so that they can be displayed correctly.
Answer by supernat · Mar 21, 2014 at 05:30 AM
I assume you are not using Unity's built-in GUI. Because that would not be affected by a camera. It's always rendered in screen space. However, there are two ways to do what you want. You can make your GUI button a child of the camera, and it won't matter where the camera is rotated, the button will follow. A second option is to create a 2nd camera to render the menu/GUI into. Here's a post that should provide enough details: http://answers.unity3d.com/questions/336671/additively-overlay-one-camera-on-another.html
The idea is to create 2 cameras. 1 camera is your main, but you set the Cull mask to ignore your UI layer (you must create this layer). You create a 2nd Orthographic camera that only renders the UI layer. You have to set the depth of the second camera to a bigger number than the depth of the main camera (or is it smaller, I can never remember, I think it's in the post link). You also have to tell the 2nd camera to only clear the depth buffer (this and the cull mask are settings on the camera in the editor's inspector). Now your 2nd camera can sit anywhere in the world with UI buttons in front of it at the proper positions. And since your 2nd camera is Orthographic, you don't have to worry about perspective issues. You basically just leave that camera alone, implement all of your UI over in front of it, and Unity will render first the main camera scene (without the UI layer), and then render the 2nd camera scene (which is only the UI layer).