Log in or register to post comments

Problems with Raycast for ARCamera

June 8, 2014 - 11:39am #1

Hey folks-

I am new to Vuforia.

I know this is not a new question and I have gone through each of the thread/post on this discussion blog for that matter but the problem still persists. And the problem seems very fundamental.

 

I have the following script attached to my gameobject :

	void Update () 
	{
		if (Input.touchCount == 1) 
		{
			// Touches performed on screen
			Ray ray;
			RaycastHit hit;
			Debug.Log ("2");
			if(Camera.main != null)
			{
				Debug.Log ("3");
				ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
				hit = new RaycastHit();
				Debug.Log ("33");
				if(Physics.Raycast(ray, out hit))
				{
					Debug.Log ("4");
				}
                       }
                }
        }

When I run the scene and touch on the gameobject, the Debug Console shows 

2

3

33

BUT NOT 4. Somehow this ray doesn't hit the object. 

 

This script works fine with the normal camera. Could anyone please shed some light on this.

 

Thanks

 

Problems with Raycast for ARCamera

October 23, 2019 - 7:10am #6

Hi @mazher @mocom,

I've used the example code from Physics.Raycast Unity Scripting API, counted the hits and added an UI to show me on the device if it's happening or not. I can confirm it works as expected.

code below:

public class Raycast : MonoBehaviour

{

    int count = 0;

     public Text scoreText;

    void Update()

    {

        // Bit shift the index of the layer(8) to get a bit mask

        int layerMask = 1 << 8;

        // This would cast rays only against colliders in layer 8.

        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.

        layerMask = ~layerMask;

        RaycastHit hit;

        // Does the ray intersect any objects excluding the player layer

        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))

        {

            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);

            Debug.Log("Did Hit");

            count++;

            scoreText.text = count.ToString();

        }

        else

        {

            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);

            Debug.Log("Did not Hit");

        }

    }

I've added the script to the ARCamera.

Tested on an Android Device.

Hope it helps.

Thank you.

Vuforia Engine Support

Problems with Raycast for ARCamera

October 20, 2019 - 4:58pm #5

Did you manage to get this working? I am having the same issue.

Problems with Raycast for ARCamera

October 19, 2019 - 12:19am #4

Hi, did you manage to find a solution to this?

Problems with Raycast for ARCamera

February 25, 2018 - 10:29am #3

I am facing the same problem. Raycast is not working with ARCamera.  

Note:  Raycast is working perfectly fine on Unity Editor but not working on device.

   Please advice me what to do I have already waisted a lot of time trying fixing this.

   RaycastHit hitInfo;

   int layerMask = 1 << 8;

   if (Physics.Raycast (arCamera.ScreenPointToRay ( Input.mousePosition), out hitInfo, Mathf.Infinity, layerMask)) {

     Debug.Log ("Physics.Raycast (ray, out hitInfo)");

    }

Problems with Raycast for ARCamera

June 11, 2014 - 7:25am #2

Hi,

have you checked this:

https://developer.vuforia.com/resources/dev-guide/unity-drag-ar-object-screen-your-finger

This is an example of how the Unity RaycastHit technique can be used with a Vuforia ARCamera, and this has been tested and verified that works. The ARCamera per-se should have no issue with raycasting... of course there other elements that might play a role, such as the Max picking distance of the raycast;

for example, you may want to use:

Physics.Raycast(ray, out hit, maxPickingDistance)

(and choose an appropriate value for the maxPickingDistance parameter),  instead of:

Physics.Raycast(ray, out hit)

I hope this helps.

Log in or register to post comments