Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Other Recenter code snippet that works

Discussion in 'VR' started by dwatt-hollowworldgames, Dec 14, 2022.

  1. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104
    I was working with openxr and the index and ran into an issue where it doesn't center. So I came up with this code

    Code (CSharp):
    1.  
    2. if (Camera.main)
    3. {
    4.        Camera main = Camera.main;
    5.        //get main camera which is headset
    6.         Quaternion quaternion = Quaternion.Inverse(main.transform.localRotation);
    7.         //get inverse rotation
    8.         Transform transform = this.transform;
    9.         transform.localPosition = quaternion * -main.transform.localPosition;
    10.         //local position of headset is oriented toward direction of headset so undo that before applying
    11.         transform.localRotation = quaternion;
    12. }
    13.  
    It uses a camera rig game object which is parent of the camera and hands. Just place it on a gameobject on the camera rig and call this code to center the tracking space.

    Consider it an early Christmas Present.