Search Unity

Placing a dialog box in front of VR camera

Discussion in 'AR/VR (XR) Discussion' started by rickblacker, Jul 12, 2018.

  1. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Hey all, I'm using VRTK and trying to place a dialog type panel in front of the VR camera at runtime. This dialog box gets turned on and off from a controller button click. I want it to stay in front of the VR camera. This script SEEMED to work at first, but I modified something yesterday and I have NO clue what it is. I've played with all three canvas render modes for this UI panel. But, when I activate this script. My panel always ends up behind the VR camera.
    This script ensures the dialog box is active, if so gets the headset and sets the dialog box transform settings based on the VR camera's transform. And trying to place it out in front via the * 200. I've even tried multiplying by a -200 with the thought that it would reverse the direction of "forward" and place it in front rather than behind. Adding the infoTextBox as a child of the camera didn't help. Makes not difference if it's a child or not.


    Code (CSharp):
    1.    
    2. private void Update()
    3. {
    4.     if (infoTextBox.gameObject.activeSelf)
    5.     {
    6.         _headsetTransform = VRTK_DeviceFinder.HeadsetCamera();
    7.         if (_headsetTransform != null)
    8.         {
    9.             infoTextBox.transform.parent    = _headsetTransform;
    10.             infoTextBox.transform.position  = _headsetTransform.position + _headsetTransform.forward * 200;
    11.             infoTextBox.transform.rotation  = new Quaternion(0.0f, _headsetTransform.rotation.y, 0.0f, _headsetTransform.rotation.w);
    12.         }
    13.     }
    14. }
    15.  
     
    glenneroo likes this.
  2. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    You do not need to use a script for that.
    Just set the canvas as a child of your VR camera and set the Canvas Render Mode to "World Space"
    Adjust the size and position of the canvas to fit your needs and you're done (You may need to downscale it a lot as th default canvas size is way too big)

    But depending on what you want to do it is not really a good idea to put UI element like this as it will be difficult to read.

    (see https://unity3d.com/fr/learn/tutorials/topics/virtual-reality/user-interfaces-vr for more details on VR UIs)
     
  3. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Hi goddatr,
    I had already figured it out in script, but I'm going to take a look at your suggestion. If I don't need to script it, that would save potential bugs. :)

    Thanks for the link, heading there now to read it.