Create objects with ground plane detection only once with Vuforia & Unity

混江龙づ霸主 提交于 2020-01-03 11:34:25

问题


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.


回答1:


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:




回答2:


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.




回答3:


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

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




回答4:


For Updated Versions:

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




回答5:


please try the vuforia website for this problem

Introduction to Ground Plane in Unity




回答6:


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)

  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:

I hope that helps



来源:https://stackoverflow.com/questions/48635957/create-objects-with-ground-plane-detection-only-once-with-vuforia-unity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!