Physics.Raycast and Debug.DrawRay not working with Google Cardboard / Google VR? Or because within a Delegate?

I have a Physics Raycaster attached to the Camera. The Pointer Click Event Trigger is working correctly. However I need to do it from the source code. These are my attempts:

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!” is shown in the Console. Unfortunately “Collision detected!” is not. However the Pointer Click Event Trigger is working correctly (the component attached in the inspector). How can I know what is going on? Why isn’t it working?


Moreover, these are my attempts to see Debug.DrawRay working. I cannot see any line drawn:

		Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.green, 2, false);
		Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.yellow, 2, true);
		Debug.DrawRay(head.GetComponent<GvrHead>().Gaze.GetPoint(0), head.GetComponent<GvrHead>().Gaze.GetPoint(5), Color.red, 2, true);

		Debug.DrawRay(cameraGameObject.transform.position, cameraGameObject.transform.forward, Color.green, 2, false);

		Ray ray = Camera.main.ViewportPointToRay (new Vector3(0.5f,0.5f,0f));
		Debug.DrawRay(ray.origin, ray.direction * 100, Color.white);
		Debug.DrawRay(ray.origin, ray.direction * 10);
		Debug.DrawRay(ray.origin, (ray.direction + new Vector3(0.1f,0.2f,0.3f)) * 10);

		Ray ray2 = cameraGameObject.GetComponent<Camera>().ViewportPointToRay (new Vector3(0.5f,0.5f,0f));
		Debug.DrawRay(ray2.origin, ray2.direction * 100, Color.white);

Debug.DrawRay was working well since the beginning. I did not see it because I was watching the Game view, and the ray is drawn in the Scene view! @Mmmpies also commented that we can use LineRenderer to draw a line in the Game view.

On the other hand, Physics.Raycast was not working because the target was an UI element. It seems Physics.Raycastonly works if the target has a collider attached.


So that is the first solution proposed. I used the Box Collider. Don’t forget to correct the Size! Also, don’t forget to add the Physics Raycaster script to the main camera. I used the following code and configured the Box Collider as follows:

RaycastHit hit;
if (Physics.Raycast (Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity)) {
        Debug.Log("Collider detected! 3D collider works!");
}

71826-screen-shot-2016-06-09-at-231957.png


The second solution proposed is dealing with the UI elements with its specific raycaster, UnityEngine.EventSystems.EventSystem.current. I only was able to make it work if the target has the Button and Raw Image components attached. The code and the screenshot follows:

GameObject hit2 = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
if (hit2 != null){
      Debug.Log("UI detected!");
} 

71827-screen-shot-2016-06-09-at-234140.png


The third solution is maybe the best, who knows. We have to attached to our target a Box Collider 2D component. Then using Physics2D.Raycast. Don’t forget to correct the size either. I set x and y to 200, and options unticked (sorry, this site does not allow me to add more than 2 attachments). Finally, we should add the Physics 2D Raycaster to the main camera. Here is the code:

Ray ray3 = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
RaycastHit2D hit3 = Physics2D.Raycast (ray3.origin, ray3.direction, Mathf.Infinity);
if (hit3.collider != null) {
     Debug.Log("RaycastHit2D hit collision!");
}

hey, i want the same procedure for my game. i want that when main camera’s reticle pointer hits the object, it will destroy. please help me from the start i’m bit confused. @chelder