Search code examples
unity-game-enginevirtual-reality

How to get VR HMD position without updating the camera view?


I am accessing VR HMD using XR-Plugin management system on Unity 2022 and using Tracked Pose Driver to update camera rotation with HMD rotation. I don't want to update camera position with respect to HMD so Tracking Type is set to Rotation Only mode. However I need to get HMD local position while doing that (so that camera view is not updated with the position) in order to do some stuff if there is too much head movement, acceleration etc. How do I get correct HMD local position without updating the camera view?

note: When I try xrCamera.transform.localPosition while tracking type is Rotation Only, I keep getting (0,0,0) as a result, which is of course useless.


Solution

  • In a custom script, you can add an InputActionProperty, and process its value in Update. If it's not enabled somewhere else, you need to do it yourself. Something like that:

    [SerializeField] private InputActionProperty _hmdPosition;
    
    private void Start()
    {
        _hmdPosition.action.Enable();
    }
    
    private void Update()
    {
        Vector3 pos = _hmdPosition.action.ReadValue<Vector3>();
        // do stuff
    }
    

    In the inspector you need to set it by reference or set directly in the InputActionProperty (ActionType: Value, ControlType: Vector3 and bind centerEyePosition [XR HMD]).