0

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

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

1 回答 1

0

您不应直接更改 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;
        }
    }
}
于 2019-07-22T19:45:50.003 回答