unity-game-engineaugmented-realityeasyar

How to instantiate objects in a specified place in unity?


I have an AR app that displays objects when it detects a QR code. The way I do it is with an empty object called model caller that has an script that instantiates a model, this is the script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Load_Models: MonoBehaviour
{
    // Start is called before the first frame update
    [Serializable]
    public class PlaceModel
    {
        public string Clave = "";
    }

    public GameObject[] model;

    public string[] clave_modelos;

    public string URL;

    public GameObject ModeloUI;

    public PlaceModel placeModel;

    Dictionary<string, GameObject> strGO = new Dictionary<string, GameObject>();


    public void Start()
    {
        
         StartCoroutine(GetRequest(URL));
        for (int i = 0; i < model.Length; i++)
        {
            strGO.Add(clave_modelos[i], model[i]);
        }
        

    }
    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string jsonForm = uri;

            if (webRequest.isNetworkError)
            {
                Debug.Log("Error loading");

            }
            else
            {
                try
                {

                    PlaceModel model_1 = JsonUtility.FromJson<PlaceModel>(webRequest.downloadHandler.text); 
                    Instantiate(strGO[model_1.Clave], new Vector3(0, 0, 0), Quaternion.identity, transform); //instantiates the model
                    Debug.Log("Loaded");
                }
                catch
                {
                    Debug.Log("Error in connection");
                }
            }

        }
    }
}

and this is what happens when I detect more than 1 QR (it also happens with only one, just without the models "fusing" with each other):

enter image description here

Explanation: It should display 3 models, 1 simple street (the white block) and 2 "no model" 3d texts, but, the idea is for the models to appear "attached" (I don't know how to word it) to the QR code. And I tried to do it by having model caller as a child of the ImageTarget and with model caller being in the dead center of the imagetarget, also with new Vector3(0, 0, 0).

Is there a way to do this?

I know that I can do it by simply using the prefab by itself instead of a script, but I need for the models to change depending on a website (which I already did).

I'm using EasyAR 3 for this


Solution

  • If I got you right, try to change Instantiate line this way:

    Instantiate(strGO[model_1.Clave], transform.position, Quaternion.identity, transform);
    

    Also, maybe, you dont need to set parent transform, it depends from your implementation.