Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Steam VR Plugin : Reset Position and orientation

Discussion in 'AR/VR (XR) Discussion' started by Timothee35, Mar 3, 2016.

  1. Timothee35

    Timothee35

    Joined:
    Jul 21, 2015
    Posts:
    30
    Hello,

    I'm currently working with Steam VR Plugin for Unity and i'm converting my whole project for the fantastic HTC Vive Pre and forward.

    UNFORTUNATELY i didn't figured out the equivalent to :
    " UnityEngine.VR.InputTracking.Recenter(); "

    I tried SteamVR.instance.hmd.ResetSeatedZeroPose(); without any success...
    Seems on the web i'm not alone but didn't found any answer.

    Does anyone can help ?
     
  2. Kunris

    Kunris

    Joined:
    Feb 9, 2012
    Posts:
    29
    This problem may be Unity5.3.3 Bug
     
  3. Psyco92

    Psyco92

    Joined:
    Nov 15, 2013
    Posts:
    14
    Code (CSharp):
    1.     public KeyCode reOrient;
    2.  
    3.     void Update()
    4.     {
    5.         if (Input.GetKeyDown(reOrient) == true)
    6.         {
    7.             Valve.VR.OpenVR.System.ResetSeatedZeroPose();
    8.  
    9.             //transform.parent.localEulerAngles -= transform.localEulerAngles + transform.parent.localEulerAngles;
    10.         }
    11.         //transform.parent.localPosition = -transform.localPosition;
    12.     }
    Valves reset seems like the way to go, but it does nothing I can detect. Use the commented lines and remove/comment the ResetSeatedZeroPose line, make sure the camera (origin) is a child of something that looks at your centre. And dont forget to set a reOrient key in the inspector (This code can create strange offsets when reorienting)
     
    Last edited: Jun 1, 2016
    NeoTokyo_Nori likes this.
  4. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    373
  5. gstarch

    gstarch

    Joined:
    Mar 21, 2015
    Posts:
    4
    I spent a lot of time looking for a answer on this. Tried all of the options on this thread and the miracle one without success. Looked like several of the SteamVR/Valve and native solutions stopped working after a recent update. 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.     [Tooltip("Desired head position of player when seated")]
    5.     public Transform desiredHeadPosition;
    6.     private Transform steamCamera;
    7.     private Transform steamController;
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.         if (Input.GetKeyDown(KeyCode.Space))
    13.         {
    14.             if (desiredHeadPosition != null)
    15.             {
    16.                 ResetSeatedPos(desiredHeadPosition);
    17.             }
    18.             else
    19.             {
    20.                 Debug.Log("Target Transform required. Assign in inspector.");
    21.             }
    22.        
    23.         }
    24.     }
    25.  
    26.     private void ResetSeatedPos(Transform desiredHeadPos){
    27.  
    28.         //find VR Camera and CameraRig transforms in scene
    29.         steamCamera = FindObjectOfType<SteamVR_Camera>().gameObject.transform;  
    30.         steamController = FindObjectOfType<SteamVR_ControllerManager>().transform;
    31.  
    32.         if ((steamCamera != null) && (steamController != null))
    33.             {
    34.             //{first rotation}
    35.                 //get current head heading in scene
    36.                 //(y-only, to avoid tilting the floor)
    37.                 float offsetAngle = steamCamera.rotation.eulerAngles.y;
    38.                 //now rotate CameraRig in opposite direction to compensate
    39.                 steamController.Rotate(0f, -offsetAngle, 0f);
    40.  
    41.             //{now position}
    42.                 //calculate postional offset between CameraRig and Camera
    43.                 Vector3 offsetPos = steamCamera.position - steamController.position;
    44.                 //reposition CameraRig to desired position minus offset
    45.                 steamController.position = (desiredHeadPos.position - offsetPos);
    46.  
    47.                 Debug.Log("Seat recentered!");
    48.             }
    49.         else
    50.             {
    51.                 Debug.Log("Error: SteamVR objects not found!");
    52.             }
    53.     }
    54. }
    55.  
    Turned out to be simpler than I expected. Hope it can save someone else the time!

    Cheers,

    Gerry

    PS: Running Unity 2017.2
     
    Flobiwan likes this.
  6. Paalo

    Paalo

    Joined:
    Jan 20, 2016
    Posts:
    3
    The solution @gstarch suggested is the way to go. It works both for Room Scale & Seated Experiences.

    Some things to remember though:
    • An important change is that the
      SteamVR_ControllerManager
      -script isn't attached to the
      [CameraRig]
      -prefab coming with the SteamVR-plugin anymore (in SteamVR version 1.2.3 and up).
      • This also means that the variable
        steamController
        is supposed to be set to the
        [CameraRig]
        -prefab coming with the SteamVR-plugin.
    • How Unity.XR works:
      • The function
        UnityEngine.XR.InputTracking.Recenter();
        ONLY works for Seated/Standing Experiences, ie. it won't do anything if you have set up a Room Scale-experience.
    Here's our modified version of @gstarch's solution. Works with
    Unity 2018.3.0b11
    &
    SteamVR Version 1.2.3


    Hope this helps someone!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayAreaController : MonoBehaviour
    4. {
    5.     [Tooltip("Desired head position of player when seated")]
    6.     public Transform desiredHeadPosition;
    7.  
    8.     //Assign these variables in the inspector, or find them some other way (eg. in Start() )
    9.     public Transform steamCamera;
    10.     public Transform cameraRig;
    11.  
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.Space))
    15.         {
    16.             if (desiredHeadPosition != null)
    17.             {
    18.                 ResetSeatedPos(desiredHeadPosition);
    19.             }
    20.             else
    21.             {
    22.                 Debug.LogError("Target Transform required. Assign in inspector.", gameObject);
    23.             }
    24.         }
    25.     }
    26.  
    27.     private void ResetSeatedPos(Transform desiredHeadPos)
    28.     {
    29.         if ((steamCamera != null) && (cameraRig != null))
    30.         {
    31.             //ROTATION
    32.             // Get current head heading in scene (y-only, to avoid tilting the floor)
    33.             float offsetAngle = steamCamera.rotation.eulerAngles.y;
    34.             // Now rotate CameraRig in opposite direction to compensate
    35.             cameraRig.Rotate(0f, -offsetAngle, 0f);
    36.  
    37.             //POSITION
    38.             // Calculate postional offset between CameraRig and Camera
    39.             Vector3 offsetPos = steamCamera.position - cameraRig.position;
    40.             // Reposition CameraRig to desired position minus offset
    41.             cameraRig.position = (desiredHeadPos.position - offsetPos);
    42.  
    43.             Debug.Log("Seat recentered!");
    44.         }
    45.         else
    46.         {
    47.             Debug.Log("Error: SteamVR objects not found!");
    48.         }
    49.     }
    50. }
     
    Last edited: Dec 5, 2018 at 9:26 AM
    Boemak likes this.
  7. Boemak

    Boemak

    Joined:
    May 29, 2013
    Posts:
    26
    This is just what I needed. Thanks.