6
votes

I am trying to create an AR app using Unity & Vuforia. I have a 3D model that needs to be spawned when ground plane is detected.But this needs to happen only once. The way Vuforia work is, it keeps on spawning objects when new plane is detected. So what i need to do is either detect plane only once or spawn the object only once. As i am new to Unity, i need help doing this. Great if someone could tell me what i need to do to achieve this.

6

6 Answers

8
votes

Vuforia has updated.Now There is no DeploymentStageOnce script.Inorder to stop duplicating while we touch, we have to turn off Duplicate Stage in Content Positioning Behaviour (Script)Check the Inspector when we click Plane Finder. enter image description here

3
votes

In your app you should have a Plane Finder object somewhere with the following properties set by default
enter image description here

The Plane Finder object has a Behaviour component attached that calls a Position Content method if a plane was found. That method belongs to the Content Positioning Behaviour and it makes an instance (Clone) of your Ground Plane Stage. In order to avoid more than one instance you should import the vuforia Deploy Stage Once script located here: https://library.vuforia.com/articles/Solution/ground-plane-guide.html and you should change the Plane Finder Behaviour as the following:enter image description here

2
votes

I struggled a long with it, in short we must disable AnchorInputListenerBehaviour after hit.

  1. I attached a new script on PlaneFinder with this code below:

    <!-- language-all: c# -->
    public void OnInteractiveHitTest(HitTestResult result)
    {
        var listenerBehaviour = GetComponent<AnchorInputListenerBehaviour>();
        if (listenerBehaviour != null)
        {
            listenerBehaviour.enabled = false;
        }
     }
    
  2. I added event on Plane Finder Behavior

enter image description here

That's all, I hope it will be useful.

1
votes

For Updated Versions:

enter image description here

go to "Advanced" setting and "On Interactive Hit Test" script -> Select "Off" option for the script.

1
votes

Most of the answers are correct but kind of obsolete, the correct way to do that is by code.

Create for example a gameObject called GameManager and pass the GroundPlaneStage and a prefab of the object you want to spawn to a script attached to that GameManager for example call it GameManagerScript.cs, and create a small function called spawnObjects which does the following:

public class SceneManagerScript : MonoBehaviour {

    public GameObject objPrefab;
    public GameObject ground;
    private int count = 0;

    public void spawnObject() {
        Instantiate(objPrefab, new Vector3(count, 0, 0), Quaternion.identity, ground.transform);
    count += 2; 
    }
}

then after that go to the PlaneFinder specifically to the PlaneFinderBehaviour.cs component you will have callbacks for OnInteractiveHitTest and OnAutomaticHitTest, in your case you need the OnAutomativeHitTest, click + and add a new callback (the function spawnObject in code above like in the image below)

enter image description here

  1. also when you instantiate the object of your preference via the prefab don't forget to write the proper position updates to prevent the objects from getting added in the same position
  2. also don't forget to make the GroundPlaneStage the parent of the object and realize that the position you are adding in the Instantiate() function is relative to that parent (GroundPlaneStage which is represented in the code above with the variable ground)
  3. Finally don't forget to uncheck Duplicate Stage from the "Content Positioning Behaviour" component in the Plane Finder as shown in the picture below:

enter image description here

I hope that helps

0
votes

please try the vuforia website for this problem

Introduction to Ground Plane in Unity