2
\$\begingroup\$

I'm creating a game similar to Banner Saga, except my game will be in 3D, with a camera that can move around and zoom in and out. What I want is for a menu to appear after the user clicks on a unit. This menu must always be visible and facing the camera. enter image description here

The menu that has the foot and sword symbols is what I want.

How can I get a similar result in 3D? The new Unity UI canvas isn't really suitable, since the camera will move around, and the menu must stay next to the unit. The menu elements can't be covered up by things in front of it, like other units. Thanks

\$\endgroup\$
2
\$\begingroup\$

I can suggest:

1.make menu objects using gameObjects (like quads with textures (EDIT) or better as stated by Philipp in his comment and Exerion in his answer: "using Unity5 UI system: Canvas component, which is the base of the new UI system, can be set to "World Space" to make it appear as a diegetic interface with a 3d position in the scene.") and assign them a layer.

2.use a second camera child of main camera and with all the same properties except Culling Mask setted to render only menu objects(1).

3.set the "menuCamera" Depth higher than ManiCamera so that it render menu objects on top of other stuff. EDIT (as DeeCeptor said in comment) : The clear flags on the foreground (menu camera) must be set to 'Depth only'.

4.You can regroup all menu objects under a "rootMenu" (empty gameobject). Put a script into rootMenu to make it rotate always to menuCamera

void Update() {
    // Rotate the camera every frame so it keeps looking at the target 
    transform.LookAt(menuCamera);
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ This worked! The clear flags on the foreground (menu camera) also needed to be set to 'Depth only' \$\endgroup\$
    – DeeCeptor
    Sep 16 '15 at 23:04
  • \$\begingroup\$ I missed that. Answer edited. \$\endgroup\$ Sep 17 '15 at 6:27
  • 1
    \$\begingroup\$ I recommend using the new Unity5 UI system instead of a quad with textures. The Canvas component, which is the base of the new UI system, can be set to "World Space" to make it appear as a diegetic interface with a 3d position in the scene. \$\endgroup\$
    – Philipp
    Sep 17 '15 at 9:11
  • \$\begingroup\$ edited including Unity5 UI system canvas \$\endgroup\$ Sep 17 '15 at 9:37
2
\$\begingroup\$

It looks like you need UI with render mode World Space. In that case all UI elements will be placed in world's coordinates as such other GameObjects. What you should try is:

  • Create UI Canvas, reset it's transform and set it as child of a unit you want to show
  • In that canvas create, for example, a Button and reset it's transform
  • Create simple script to make new UI behave like a billboard:

    public class LookAtCamera : MonoBehaviour
    {
        public Camera target;
    
        void Update()
        {
            if (target != null)
            {
                transform.rotation = target.transform.rotation;
            }
        }
    }
    

    To fix possible mirror effect, you can try this variation of Update method:

    void Update()
    {
        transform.LookAt(transform.position + target.transform.rotation * Vector3.back, target.transform.rotation * Vector3.up);
    }
    
  • As it done, attach this script to UI Button and set target to your UI camera (probably main camera)

At this point your button should appear near/inside your unit and may be relatively big. Adjust scale of a Button and it's position. With script work fine you should see a button near unit that always facing target camera and moving along with unit.

\$\endgroup\$
2
  • \$\begingroup\$ This does not ensure the menu is never covered by other units, does it? \$\endgroup\$
    – wondra
    Sep 15 '15 at 12:42
  • \$\begingroup\$ @wondra, exactly. Menu items can be covered by other objects in World Space. To ensure oposite behaviour you need to create another Camera, different from Camera.main, set it up as camera for UI and set target in the script to the new camera. Lastly, you need to set Culling Mask of new camera to render only UI. \$\endgroup\$
    – Exerion
    Sep 15 '15 at 14:24
0
\$\begingroup\$

You could create the GUI and disable it. After position the GUI by usin ScreenPointToRay(MOUSE POSITION) to spawn it at the position of the mouse and then enable it. Read more here: http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.