aditya parikh
aditya parikh

Reputation: 595

Problems with Raycast for ARCamera in Unity3d with Vuforia SDK

I am new to Vuforia.

The gameobject to which the script is added, is a 3d object which is made visible on user-defined triggered image.

I know this is not a new question and I have gone through each of the thread/post on official Vuforia 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

Upvotes: 1

Views: 3498

Answers (3)

Felipe B. Florez
Felipe B. Florez

Reputation: 51

If you are trying take a hit with RayCast on 3d model, you should sure add Box Collider Component on 3d model.

Upvotes: 3

Mario Alzate
Mario Alzate

Reputation: 370

I think this is a bug between Collider class and ARCamera, but the solution is this:

  1. Create a new Scene
  2. Create a Cube or any gameobject with collider component.
  3. NOT delete the Cube for any reason
  4. Test with any hit algorithm(touch or mouse)

    using System.Collections;
    using UnityEngine;
    
    public class rayoPrueba : MonoBehaviour {
    void start () {print("entro"); }
    void Update() {
    
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, 100))
                print("Si le jue");
            }
        }
    
  5. Replace the mainCamera for ARCamera

  6. Test Again
  7. Put u`r Cube inside ImageTarget and the real model
  8. Delete the Cube and lets dance!! I dont know why, but the bug is killed with this ...(Y) ..

The trick is... Never lose a gameObject with Collider Component from the scene..

Upvotes: 3

user2549327
user2549327

Reputation:

(as far as I can tell) Vuforia doesn't use the ARCamera for collision detection. Instead there is another 'Background Camera' (you can see it if you run your app in Unity and pause it; you'll find it in the Hierarchy pane). To access it use

Camera.allCameras[0] 

instead of

Camera.main

Hope that helps

Upvotes: 4

Related Questions