Search code examples
c#unity-game-enginevirtual-realityoculus

Unity reset Oculus position when key is pressed


I am trying to create a script that will reset (to a specific location) the HMD and controller locations whenever a key is pressed for calibration reasons. I am very new to unity so all I have been able to figure out is how to get key input.

public class resetPosition : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            Debug.Log("pressed");
    }
}

Solution

  • You shouldn't directly change the position of the VRCamera.

    Rather add a parent GameObject to the camera and change the position of that one instead via e.g. (assuming your script is attahced to the camera)

    public class ResetPosition : MonoBehaviour
    {
        public Vector3 resetPosition;
    
        private void Awake()
        {
            // create a new object and make it parent of this object
            var parent = new GameObject("CameraParent").transform;
    
            transform.SetParent(parent);
        }
    
        // You should use LateUpdate
        // because afaik the oculus position is updated in the normal
        // Update so you are sure it is already done for this frame
        private void LateUpdate()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Debug.Log("pressed");
    
                // reset parent objects position
                transform.parent.position = resetPosition - transform.position;
            }
        }
    }