Log in or register to post comments

calculating AR Camera distance to gameobject/fiducial

April 5, 2012 - 1:52pm #1

Dear Community
We are doing a project in unity for android.
In this project we need to check the distance from the AR camera to the gameobject/fiducial in order to make some of the objects invisible when you move the phone closer to the fiducial and some other object visible in stead. You can call it some sort of "level of detail".

So my question is how to calculate the distance between AR camera with fiducial/gameobject. So far im using something like this:

void Update() {
float dist = Vector3.Distance(AR.position, transform.position);
}

But it dont seem to help.
Hope some of you may be able to help us. we will appreciate it a lot.

Re: calculating AR Camera distance to gameobject/fiducial

April 6, 2012 - 9:05am #4

This seems to work fine, although I had to change the distance check to 250 for it to work reasonably with the ImageTargets sample.

Note that this script is going to overwrite the DefaultTrackableEventHandler, which turns the renderers on/off when the target is tracked/lost. This may result in the model appearing even when tracking is lost.

TrackerEventHandler.cs should be attached to the ARCamera (see the ImageTargets sample, it is already attached there).

- Kim

Re: calculating AR Camera distance to gameobject/fiducial

April 6, 2012 - 4:32am #3

Thank you for your reply.
I am calculating the distance in a script attached to the image target. It seems to disappear and reappear when i run it in unity and drag the AR camera away and towards the imagetarget with no problems.
But it does not do so when i build and run it on the phone. on the phone the model is always visible.

Here is the code in the script attached to the imagetarget:
void FixedUpdate() {
float dist = Vector3.Distance(AR.position, transform.position);
Debug.Log(dist);
if(dist MeshRenderer[] meshRenderers = GetComponentsInChildren();
foreach (MeshRenderer MR in meshRenderers)
{
MR.enabled = false;
}
}
else {
MeshRenderer[] meshRenderers = GetComponentsInChildren();
foreach (MeshRenderer MR in meshRenderers)
{
MR.enabled = true;
}
}

The model is a child of the image target as well.

So it works in unity but not when i compile it on the phone. there my model is always visible.

You suggested using TrackerEventHandler.cs instead. Where should i attach that script to?

Hope you guys have the time to help us.

Re: calculating AR Camera distance to gameobject/fiducial

April 5, 2012 - 4:14pm #2

Seems like you're close, you want the distance between the ARCamera's transform.position and the Trackable's transform.position. For the best results I suggest doing this in the OnTrackablesUpdated callback of the ITrackerEventHandler interface. See the TrackerEventHandler.cs script in the ImageTargets sample.

- Kim

Log in or register to post comments