flutter-unity-arkit-demo icon indicating copy to clipboard operation
flutter-unity-arkit-demo copied to clipboard

How to place an existing gameObject in AR Scene?

Open Linspirit98 opened this issue 4 years ago • 2 comments

Instead of dragging prefab into the inspector and instantiating it, how can I place a gameObject in AR scene.

Below is my code:

[SerializeField]
 private GameObject placedPrefab;

 [SerializeField]
 private GameObject placedObject;

 [SerializeField]
 private ARPlaneManager arPlaneManager;

 void Awake() 
 {
     dismissButton.onClick.AddListener(Dismiss);
     arPlaneManager = GetComponent<ARPlaneManager>();
     arPlaneManager.planesChanged += PlaneChanged;
 }

 private void PlaneChanged(ARPlanesChangedEventArgs args)
 {   
     if(args.added != null && placedObject == null)
     {
         ARPlane arPlane = args.added[0];
         //placedObject = Instantiate(placedPrefab, arPlane.transform.position, Quaternion.identity);
         placedPrefab.transform.position = arPlane.transform.position;
     }
 }

Linspirit98 avatar Aug 11 '20 03:08 Linspirit98

Hi, @Linspirit98 It would be really great if you can be more specific about your question...Maybe you can explain using an example scenario.

Jatin-Pawar avatar Aug 11 '20 06:08 Jatin-Pawar

Hi, @Linspirit98 It would be really great if you can be more specific about your question...Maybe you can explain using an example scenario.

Hi. So I would like to load a gameObject from a scene into my AR scene. The AR scene will automatically detect for the plane. Once a plane is detected, the gameObject loaded should be automatically placed on the plane. The code I found uses prefab and instantiate like this placedObject = Instantiate(placedPrefab, arPlane.transform.position, Quaternion.identity);

However, if the placed prefab is of type gameObject (basically I dragged the gameobject loaded), it didnt work. the gameobject was floating in air instead of following the detected plane. Do you know whether it is possible to achieve my desired scenario? Why is the position of gameobject wrong when I drag an existing gameobject in the scene instead of prefab?

Best regards.

Linspirit98 avatar Aug 11 '20 23:08 Linspirit98