Search Unity

How to create contextual VR Menu

Discussion in 'AR/VR (XR) Discussion' started by sajidsaiyed, Jan 14, 2018.

  1. sajidsaiyed

    sajidsaiyed

    Joined:
    Jan 10, 2018
    Posts:
    9
    Hi,
    I am trying to understand how to create a dynamic contextual menu in VR for Cardboard.

    For example, I have a scene of an interior of a house. Now when the user turns their head and the reticle points at a surface, say a wall, the entire wall is interactive. When the user activates a click, I want a menu to appear.

    Since the user could be looking in any direction, I cannot pre-define the position of the menu, it should just appear in front of the user wherever they are looking.

    When the menu appears, the user should be still able to move their head to focus on a particular menu item and select it.

    I tried using the Screen Space style menu, but when the user moves the head, the menu also moves with it and hence the user can select only one item.

    Is there something I am missing here? Should I not be using screen space on my canvas? What is the right way to do it?

    Thanks.
     
  2. Deleted User

    Deleted User

    Guest

    Screen space draws directly over the camera.
    Since VR cameras behave differently and is all about moving around freely, UI can't be drawn with the same methods you would use in a typical game / app. It was to be drawn somewhere in 3D space where the user can focus. (IE in world space) https://docs.unity3d.com/ScriptReference/Canvas-renderMode.html

    This article should have all the information you need on UI in VR :
    https://unity3d.com/learn/tutorials/topics/virtual-reality/user-interfaces-vr

    And here's a sample code that will place an object directly in front of the camera, looking at the camera. However, some shader tweaking should be done for the object to draw on top of other objects. Note that this may cause some visual strain.


    Code (CSharp):
    1. private void OnEnable()
    2.         {
    3.             if (_camera == null)
    4.             {
    5.                 _camera = Camera.main.transform;
    6.             }
    7.             if (_transform == null)
    8.             {
    9.                 _transform = transform;
    10.             }
    11.             // Director vector
    12.             Vector3 director = _camera.forward;
    13.  
    14.             // Look at rotation
    15.             Quaternion inverseRot = Quaternion.LookRotation(director);
    16.             _transform.rotation = inverseRot;
    17.  
    18.             // Position
    19.             Vector3 newPos = _camera.position + (director * _distanceToSpawn);
    20.             _transform.position = newPos;
    21.         }
     
  3. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    You need to use a world space UI, and instantiate it when the user clicks the button. The position where it spawns could be a transform that is a child of Main Camera, and moved forward on the Z axis. The parent of the UI should be null, it will be a root transform.

    I show how that works here:
    youtu.be/JzrhU3SDo3Y?t=9m33s
     
    Last edited: Jan 15, 2018
  4. sajidsaiyed

    sajidsaiyed

    Joined:
    Jan 10, 2018
    Posts:
    9
    Thanks a lot.
    Trying to understand your example code.
    What does _camera and _transform refer to?

    Pardon my basic questions, still in early stages of learning :)

     
  5. Deleted User

    Deleted User

    Guest

  6. a01730523_unity

    a01730523_unity

    Joined:
    Sep 30, 2020
    Posts:
    1
    Does anyone knows how to do this step in Creating a VR Menu tutorial?
    upload_2020-10-15_22-1-17.png