Help needed with a script to enable - disable camera within a distance form AR marker

Hello to all you geniuses. I’m new to Unity3d and scripting and I’m trying really hard to make an AR application for Android mobiles. I came up to a problem, that cannot solve no matter what I’ve though of.

Here is the situation. I have an AR camera scripted with the NyARToolkit plugin for Augmented Reality for Unity3d.

This camera, tagged as “MainCamera”, needs to get switched off, when a certain distance is reached (meaning moving away the android phone) form the AR plane with the marker that renders the model on it.
When this distance is reached, I want to set off “MainCamera” and load another unity scene. Although I can get the distance and get massage on console, I cannot set off the camera.

Need to mention that, this script is attached on “MainCamera”, and this gameObject need to be disabled.
Here is the script:

#pragma strict
var mainCamera : Transform;
var camera : GameObject;
function Update () {
var distance = Vector3.Distance(mainCamera.position, transform.position);
  
    if (distance<10){
        Debug.Log ("CloseUp camera is on : " +distance);        
        }
      
    if (distance>100){
        Debug.Log ("CloseUp camera is off : " +distance);
                               
        GameObject.FindSceneObjectsOfType(Camera) == false;
    }
}

To disable the main camera GameObject try this:

#pragma strict
var mainCamera : Transform;
var camera : GameObject;
function Update() {
   var distance = Vector3.Distance(mainCamera.position, transform.position);
   if(distance<10) {
         Debug.Log("CloseUp camera is on : " + distance); 
   }
   if(distance > 100) {
      Debug.Log("CloseUp camera is off : " + distance);
      Camera.main.gameObject.SetActive(false);
   }
}

Hi @deamon3000. Thank you. It works, but it does enable-disable the camera constantly. It should be something wrong with the update function, I suppose. Can you give it a second try, to make it work o.k?

Try this:

#pragma strict
var mainCamera : Transform;
var camera : GameObject;
function Update() {
    var distance = Vector3.Distance(mainCamera.position, transform.position);
    if(distance <= 10 && !Camera.main.gameObject.activeSelf) {
        Camera.main.gameObject.SetActive(true);
    }
    else if(distance > 10 && Camera.main.gameObject.activeSelf) {
        Camera.main.gameObject.SetActive(false);
    }
}

If this doesn’t work there might be a problem with the way you calculate and handle the distance. I don’t know how your scene is set up so I can’t help you there.

Well…It didn’t work @deamon3000. Still does the same enable-disable loop, when the distance is >10. This script is attached to an empty object with a sphere collider, and the camera is positioned on the same x,y,z as this object. As the camera moves away, the distance rises above 10, and should be switched off. Any help now?

Do you have other scripts that change the state of the camera? Could there be a conflict between them?

Try this:

#pragma strict
var mainCamera : Camera;
function Update() {
    var distance = Vector3.Distance(mainCamera.transform.position, transform.position);
    if(distance <= 10 && !mainCamera.enabled) {
        mainCamera.enabled = true;
    }
    else if(distance > 10 && mainCamera.enabled) {
        mainCamera.enabled = false;
    }
}

Notice that I changed the type of the “mainCamera” variable from Transform to Camera so you’ll have to reassign it in the inspector.

Nop. No other scripts on camera that chance its state. Sad thing is, nor this script version worked. I think I should find another approach, as even with your version that flickered on/off the camera, once I had it run on android device, and move the phone away from the marker, it did not flicker the camera. I believe I should find a way to mark a certain distance as the same in Unity scene and in real world, and when the camera on android device move away, it should turned off. Any clues on this, a tutorial, or an example code sample?

I tried the last script version in Unity and it worked so it must be something AR specific(or specific to how your game works) which unfortunately I can’t help you with. Hope you find a solution.

Thank you @deamon3000. You’ve been very helpful. I’ll give it a second try using the last script version. Do you happen to know about how to find the distance between the marker and the device? So, when the distance is e.g. more than 1m, the AR camera is switched off, and only Unity camera is active? I want something similar to this app on the following link, that finds the distance form the marker in real world. Take a look, maybe you can come up with an idea? Do you think this is feasible in Unity3d, or it has to be hard coded? Here is the link.

I’m sorry but I can’t help you with AR specific problems. Maybe you can find an answer here.

Well, @deamon3000, I’m aware of this forum. Its kind of “dead”, cause nobody responds to nobody’s question. Thanks, anyway. By the way I have visited your signature links, keep up the good work!