Skip to content

How to place an existing gameObject in AR Scene?  #16

@Linspirit98

Description

@Linspirit98

Hi, my question is: Instead of dragging prefab into the inspector and instantiating it, how can I place a gameObject in AR scene?

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?

Thank you in advance!

Below is my code but it didnt work:

[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;
     }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions