Log in or register to post comments

Physics.Raycast and touching a Marker

November 29, 2011 - 11:43am #1

I have a curious problem with Physics.Raycast(ray, out hit).
I have my ARCamera set to center World Center NONE and 3 Markers. These 3 markers are tracked perfectly.
When I touch one these markers following code is issued:


ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
//Also tried this, but did not work:
 if(Physics.Raycast(ray, out hit, Mathf.Infinity))
if(Physics.Raycast(ray, out hit))
{
    string colliderName = hit.collider.name;
    Debug.Log("My Marker is " + colliderName)
}

Unfortunately when I take some distance (let's say 1,5 m) from the markers or quickly move, the collider name is incorrect taking the name of another marker but not from the one touched. With a distance of 30cm to 1m everything is working perfectly.

Is there something I need to set up in the ARCamera settings to avoid such problem with Physic.Raycast? Is this due to the World Center NONE (I've also tested it with World Center USER, but faced the same problem)?

Thanks a lot!

Re: Physics.Raycast and touching a Marker

December 3, 2011 - 10:41am #6

I've filed this as a future improvement, thanks!

- Kim

Re: Physics.Raycast and touching a Marker

December 1, 2011 - 12:20pm #5

Hi Kim,

Thanks a lot for your help and your hint. Indeed the colliders were the problem.

I ended up modifying the TrackableEventHandler.cs by writing the following lines. I now have several 3D objects (with colliders) that are children of the marker. For test purposes I've remove the collider of the marker itself. Here is now my code, Physics.Raycast now works perfectly :-)


private void OnTrackingLost()
    {
...
    Collider[] colliderComponents = GetComponentInChildren<Collider>();
        
        foreach(Collider myCollider in colliderComponents)
        {
            myCollider.enabled = false;    
        }
...
}

and of course the same in OnTrackingFound() with myCollider.enable = true;

Perhaps this piece of code could be included per default in a future release of the Unity examples, because this problem with colliders is pretty tricky and leads to curious behaviours, like the one I've described.

Re: Physics.Raycast and touching a Marker

November 30, 2011 - 9:23pm #4

Ah! I know what's going on. The default TrackableEventHandler only turns the renderer components on and off, but the colliders are still active (stealth colliders!)

Try updating the TrackableEventHandler script like this:

    private void OnTrackingFound()
    {
        ...

        collider.enabled = true;
    }

    private void OnTrackingLost()
    {
        ...

        collider.enabled = false;
    }

Let me know if that helps.

- Kim

Re: Physics.Raycast and touching a Marker

November 30, 2011 - 12:52pm #3

Hi Kim,

I've modified the lines as stated in your post.
Unfortunately the Raycast still has the problem, sometimes gives the name of markers that are not even visible or in the camera's focus.
If I slightly begin to approach (a z-translation of the iPad of about 1-2cm) towards the marker the Raycast is correct.
It seems as if there is small distance range (4 to 5cm) in which - even if the markers are not active or visible - the Raycast is incorrect and returns a incorrect collider's name.

Do you see another solution to realize a 100% correct Raycast indenpendently of the distance and the visibility of other markers?

Re: Physics.Raycast and touching a Marker

November 29, 2011 - 6:48pm #2

Take a look at this thread, the posts towards the end might offer a fix:

http://ar.qualcomm.at/node/2000861

- Kim

Log in or register to post comments