Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Instantiating multiple objects when looking at different images (ARFoundation)

Discussion in 'AR' started by jetlius, May 11, 2022.

  1. jetlius

    jetlius

    Joined:
    Feb 6, 2018
    Posts:
    9
    I'm trying to create an app that spawns (instantiates) multiple different objects in AR, depending on the image that is being detected during runtime.

    I was able to develop all the detection and spawning of the objects for each different image, and the 3D objects even follow the position of those images.

    However, I have been looking for a way to solve an issue with the rotation of those objects. Whenever I instantiate a new object, its rotation doesn't change, i.e. the rotation of the new spawned object is always the same as the first one that was detected during runtime, even if I change scenes during runtime.

    I have tried multiple ways to save the rotation of my trackedImage, and force that rotation onto the gameObject, but it just isn't working, and I don't understand why.

    I tried changing its rotation in the same place where I'm tracking the position (inside UpdateImage), I have tried inside the foreach loops, I have tried changing the rotation only when I'm instantiating, etc, and it just doesn't work.

    This is my ImageTracking script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.UI;
    7.  
    8. [RequireComponent(typeof(ARTrackedImageManager))]
    9. public class ImageTracking : MonoBehaviour
    10. {
    11.     //[SerializeField]
    12.     public GameObject[] placeablePrefabs;
    13.  
    14.     public Dictionary<string, GameObject> spawnedPrefabs = new Dictionary<string, GameObject>();
    15.     private ARTrackedImageManager trackedImageManager;
    16.  
    17.     private Transform _trackables;
    18.  
    19.     //public GameObject _canvas;
    20.     //public StartExperience _startExperience;
    21.  
    22.     private void Awake()
    23.     {
    24.         trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    25.  
    26.         //temp = FindObjectOfType<ARSessionOrigin>();
    27.  
    28.         _trackables = this.gameObject.transform.Find("Trackables");
    29.  
    30.         foreach(GameObject prefab in placeablePrefabs)
    31.         {
    32.             GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
    33.             newPrefab.transform.SetParent(_trackables);
    34.             newPrefab.name = prefab.name;
    35.             spawnedPrefabs.Add(prefab.name, newPrefab);
    36.  
    37.             foreach (GameObject go in spawnedPrefabs.Values)
    38.             {
    39.                 if (go.name != name)
    40.                 {
    41.                     go.SetActive(false);
    42.                 }
    43.             }
    44.         }
    45.     }
    46.  
    47.     private void OnEnable()
    48.     {
    49.         trackedImageManager.trackedImagesChanged += ImageChanged;
    50.     }
    51.  
    52.     private void OnDisable()
    53.     {
    54.         trackedImageManager.trackedImagesChanged -= ImageChanged;
    55.     }
    56.  
    57.     private void ImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
    58.     {
    59.         foreach(ARTrackedImage trackedImage in eventArgs.added)
    60.         {
    61.             InstantiateImage(trackedImage);
    62.         }
    63.         foreach (ARTrackedImage trackedImage in eventArgs.updated)
    64.         {
    65.             UpdateImage(trackedImage);
    66.         }
    67.         foreach (ARTrackedImage trackedImage in eventArgs.removed)
    68.         {
    69.             spawnedPrefabs[trackedImage.name].SetActive(false);
    70.         }
    71.     }
    72.  
    73.     private void UpdateImage(ARTrackedImage trackedImage)
    74.     {
    75.         string name = trackedImage.referenceImage.name;
    76.         Vector3 position = trackedImage.transform.position;
    77.  
    78.         GameObject prefab = spawnedPrefabs[name];
    79.  
    80.         prefab.transform.position = position;
    81.         //prefab.transform.rotation = Quaternion.identity;
    82.         prefab.SetActive(true);
    83.  
    84.         /*foreach(GameObject go in spawnedPrefabs.Values)
    85.         {
    86.             go.transform.rotation = trackedImage.transform.rotation;
    87.             if(go.name != name)
    88.             {
    89.                 go.SetActive(false);
    90.             }
    91.         }*/
    92.     }
    93.  
    94.     private void InstantiateImage(ARTrackedImage trackedImage)
    95.     {
    96.         string name = trackedImage.referenceImage.name;
    97.         Vector3 position = trackedImage.transform.position;
    98.  
    99.         GameObject prefab = spawnedPrefabs[name];
    100.  
    101.         prefab.transform.position = position;
    102.         //prefab.transform.rotation = Quaternion.identity;
    103.         prefab.SetActive(true);
    104.  
    105.         foreach (GameObject go in spawnedPrefabs.Values)
    106.         {
    107.             go.transform.rotation = trackedImage.transform.rotation;
    108.             /*if(go.name != name)
    109.             {
    110.                 go.SetActive(false);
    111.             }*/
    112.         }
    113.     }
    114. }
     
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    269
    KirillKuzyk likes this.