Search Unity

How to reset position on Vive ?

Discussion in 'AR/VR (XR) Discussion' started by deglet, Sep 7, 2017.

  1. deglet

    deglet

    Joined:
    Feb 4, 2016
    Posts:
    14
    Hello,
    I am making a plane game on Vive. I would like to have multiple point of views, in order to do that i changed the position of the CameraRig (too small to go on all the positions at once).
    And I would like to reset the position of the eye camera exactly on a dummy (to be sure the head is where I want). But I don't know how to do it.

    Code (CSharp):
    1. if (Input.GetKeyUp(KeyCode.M))
    2.         {
    3.             CameraRigVR.transform.parent = Placecockpit.transform;
    4.             CameraRigVR.transform.position = Placecockpit.transform.position;
    5.          
    6.             Valve.VR.OpenVR.System.ResetSeatedZeroPose();
    7.         }
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
  3. deglet

    deglet

    Joined:
    Feb 4, 2016
    Posts:
    14
    Yes can't manage to make it work ether, I fell like I miss something, is this function suppose to place the camera at is parent position? How does he know which camera, and parent, it is ?
     
  4. deglet

    deglet

    Joined:
    Feb 4, 2016
    Posts:
    14
    +This one is for Oculus, not Vive
     
  5. gstarch

    gstarch

    Joined:
    Mar 21, 2015
    Posts:
    4
    I spent a lot of time looking for an answer on this. Eventually wrote this script which took care of it to my liking.

    This script will allow a reset of the SteamVR camera rig to align with a transform representing the desired head position in the scene. Attach to any GameObject in the scene. Useful for seated experiences like cockpits.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ResetSeatedPosition : MonoBehaviour {
    4.  
    5.     [Tooltip("Desired head position of player when seated")]
    6.     public Transform desiredHeadPosition;
    7.     private Transform steamCamera;
    8.     private Transform steamController;
    9.  
    10.     // Update is called once per frame
    11.     void Update () {
    12.  
    13.         if (Input.GetKeyDown(KeyCode.Space))
    14.         {
    15.             if (desiredHeadPosition != null)
    16.             {
    17.                 ResetSeatedPos(desiredHeadPosition);
    18.             }
    19.             else
    20.             {
    21.                 Debug.Log("Target Transform required. Assign in inspector.");
    22.             }
    23.        
    24.         }
    25.     }
    26.  
    27.     private void ResetSeatedPos(Transform desiredHeadPos){
    28.  
    29.         //find VR Camera and CameraRig transforms in scene
    30.         steamCamera = FindObjectOfType<SteamVR_Camera>().gameObject.transform;  
    31.         steamController = FindObjectOfType<SteamVR_ControllerManager>().transform;
    32.  
    33.         if ((steamCamera != null) && (steamController != null))
    34.             {
    35.             //{first rotation}
    36.                 //get current head heading in scene
    37.                 //(y-only, to avoid tilting the floor)
    38.                 float offsetAngle = steamCamera.rotation.eulerAngles.y;
    39.                 //now rotate CameraRig in opposite direction to compensate
    40.                 steamController.Rotate(0f, -offsetAngle, 0f);
    41.  
    42.             //{now position}
    43.                 //calculate postional offset between CameraRig and Camera
    44.                 Vector3 offsetPos = steamCamera.position - steamController.position;
    45.                 //reposition CameraRig to desired position minus offset
    46.                 steamController.position = (desiredHeadPos.position - offsetPos);
    47.  
    48.                 Debug.Log("Seat recentered!");
    49.             }
    50.         else
    51.             {
    52.                 Debug.Log("Error: SteamVR objects not found!");
    53.             }
    54.     }
    55. }
    56.  
    Turned out to be simpler than I expected. Hope it can save someone else the time!

    Cheers,

    Gerry

    PS: Running Unity 2017.2
     
    renem likes this.