Search Unity

Question Getting my HUD in front of me

Discussion in 'VR' started by lz7cjc, Oct 22, 2021.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    Hi
    I have built a basic HUD to enable me to walk around my level in VR. I have done this by placing both the HUD and the main camera at the same level within my Player object.

    However if I turn 180 degrees my HUD is now behind me. If I put the HUD inside the camera then I obviously can't get my gazecontrol to ever focus on it since it moves with the camera.

    I think I want to recentre the HUD as my camera moves, relative to the camera. But I have no idea how to do this. If this is the best solution please can you help? If there is a better solution I am all ears...

    Code (CSharp):
    1.  player.position = transform.position + Camera.main.transform.forward * speedSet * Time.deltaTime;
    2.  HUD.transform.position = player.position;
    3.  
    I am building this app without accessing buttons to ensure maximum compatibility
    thanks for any advice
    Nick
     
    Last edited: Oct 22, 2021
    tenconmar likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, my first advice would be to abandon gaze control... since Quest came out, there is very little demand for mobile-phone VR, and so the abomination that is gaze control can finally be laid to rest.

    But even with proper hand controls, you still don't really want UI stuck to the camera except for debugging, as it's just annoying. So yeah, you want it to follow you but in a lazy fashion.

    You'll do this by (1) getting a Quaternion that matches the camera rotation around the Y axis only; (2) rotating the UI's container towards this at some steady rate, as shown in the example code for Quaternion.RotateTowards.
     
    tenconmar likes this.
  3. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    thanks - i am using Cardboard as this has to work with the cheapest possible hardware, due to the nature of the project.
    Looking at Quaterion
    I have tried using this code:
    HUD.transform.rotation = Quaternion.RotateTowards(HUD.rotation, player.rotation, 5);
    to no avail

    Hierarchy
    2021-10-22 (10).png
    HUD Initial setttings 2021-10-22 (13).png

    When I try to reset position of HUD
    2021-10-22 (17).png

    Player initial settings
    2021-10-22 (19).png

    any ideas?
    thanks for your help
     

    Attached Files:

  4. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    also when i put a rigid body on the HUD object so I can use this code it starts circling my scene and the movement screen kicks in hudrotate.gif

    I am wondering if there is a cleaner approach that achieves the same purpose. Using gaze to move around my scene whilst getting access to the HUD no matter which direction i am facing
     
  5. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    You can use
    Vector3.ProjectOnPlane
    to remove the pitch up/down from the camera's forward vector and position your UI at the end of that ray. You still have to deal with the fact that you won't be able to gaze at any buttons that are left/right of dead center.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FlatForwardDemo : MonoBehaviour {
    4.  
    5.     private Vector3 flatForward;
    6.     private Transform cameraTransform;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start() {
    10.         cameraTransform = Camera.main.transform;
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update() {
    15.         flatForward = Vector3.ProjectOnPlane(cameraTransform.forward, Vector3.up).normalized;
    16.  
    17.         var ray = new Ray(cameraTransform.position, flatForward);
    18.         // world position for UI that's 1.5 meters away from user's face.
    19.         var endpoint = ray.GetPoint(1.5f);
    20.     }
    21.  
    22.     private void OnDrawGizmos() {
    23.         if (!UnityEditor.EditorApplication.isPlaying) {
    24.             return;
    25.         }
    26.  
    27.         Gizmos.color = Color.magenta;
    28.         Gizmos.DrawRay(cameraTransform.position, flatForward);
    29.     }
    30. }
    31.  
     
  6. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    I assume I'm not the first person to want to walk/stop through gaze ... Is there a standard way to do this? I've a feeling I'm barking up the wrong tree thanks
     
  7. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    tenconmar likes this.
  8. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    Thanks... That was my original approach but it increases the risk of motion sickness. By making it based on actual head movement I've found this mitigates the problem (anecdotal but I get it bad on vehicles and fixed path but not when I'm in full control)
     
  9. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    so I have gone for four huds as can't work out how to implement the preferred solution; which is fine as this is just a PoC.

    2021-11-02 (3).png
    Code (CSharp):
    1.    
    2.  public Rigidbody floor;
    3.     public Rigidbody player;
    4.        public GameObject huds;
    5. ...
    6. ...
    7. player.MovePosition(transform.position + Camera.main.transform.forward * speedSet * Time.deltaTime);
    8.  
    However when I start to move, the huds don't move with me (the player/camera) - how do i get them to remain in the same place relative to me?

    thanks