按下键时Unity重置Oculus位置

Unity reset Oculus position when key is pressed

我正在尝试创建一个脚本,每当出于校准原因按下某个键时,该脚本将重置(到特定位置)HMD 和控制器位置。我是 unity 的新手,所以我能弄清楚的是如何获得关键输入。

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");
    }
}

您不应该直接更改 VRCamera 的位置。

而是将父级 GameObject 添加到相机并通过例如更改父级的位置(假设您的脚本已连接到相机)

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;
        }
    }
}