Physics.Raycast 无法使用 Google Cardboard / Google VR

Physics.Raycast not working with Google Cardboard / Google VR

我有一个连接到相机的 Physics Raycaster。指针单击事件触发器工作正常。但是我需要从源代码中做到这一点。这些是我的尝试:

private void SetOnPushButtonFireManager(){
    cardboard.OnTrigger += () => {
        Debug.Log("Button triggered!");
        RaycastHit hit;
        // if(Physics.Raycast(headGameObject.GetComponent<GvrHead>().Gaze, out hit, Mathf.Infinity)){
        if(Physics.Raycast(cameraGameObject.transform.position, cameraGameObject.transform.forward, out hit, Mathf.Infinity)){
              Debug.Log("Collision detected!");
        }
    };
}

"Button triggered!" 显示在控制台中。不幸的是 "Collision detected!" 不是。但是,指针单击事件触发器工作正常(检查器中附加的组件)。我怎么知道发生了什么?为什么它不起作用?

更新:我在这里回答了这个答案:http://answers.unity3d.com/answers/1200449/view.html

(Whosebug 不允许我删除这个问题)

这是我用来从相机发射光线的一些代码。我没有 Google Cardboard,这是为相机和鼠标指针设置的。

   // Fire ray from camera
    float rayLength = 2f
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    // If ray hits object within length
    if (Physics.Raycast(ray, out hit, rayLength))
    {
            Debug.Log("Collision detected!:);
    }

我也没用,最后忘记把"GvrPointerPhysicsRaycaster"class放在相机里了。 Inspector Image

添加后,效果很好。

void Update()
{
    RaycastHit hit;

    if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity))
    {
        if (activeSelected == null)
        {
            if (hit.collider.tag == "Plane")
                activeSelected = Instantiate(mainApp.mainModel.preafabSelect, hit.point, Quaternion.LookRotation(hit.normal));
        } else
        {
            activeSelected.transform.position = hit.point;
            activeSelected.transform.rotation = Quaternion.LookRotation(hit.normal);
        }
    }
}