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.
In your app you should have a Plane Finder
object somewhere with the following properties set by default
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:
In your app you should have a Plane Finder
object somewhere with the following properties set by default
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:
I struggled a long with it, in short we must disable AnchorInputListenerBehaviour after hit.
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; } }
I added event on Plane Finder Behavior
That's all, I hope it will be useful.
For Updated Versions:
go to "Advanced" setting and "On Interactive Hit Test" script -> Select "Off" option for the script.
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)
- 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
- 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)
- Finally don't forget to uncheck Duplicate Stage from the "Content Positioning Behaviour" component in the Plane Finder as shown in the picture below:
I hope that helps
please try the vuforia website for this problem
© 2022 - 2024 — McMap. All rights reserved.