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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved AR Translation interactable destroys AR tracked Image when used by an instantiated prefab

Discussion in 'AR' started by rsauchuck-gp, Dec 1, 2022.

  1. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    I have an iOS AR app that tracks multiple images and instantiates a prefab over each as a child. The prefabs include several AR components so the user can scale, rotate, and translate them. If I move the tracked image, the instantiated prefabs update their positions so that they remain over their parent images EXCEPT if the user has translated (dragged) the prefab. If they have, then the prefab does NOT change position and instead remains where it was in world space.
    Does the AR Translatable component break this parenting?
    How can I make sure that the prefabs properly update their positions?
    Do I need to add code to the tracked images update event to specifically reset the prefabs position/parent?
     
    Last edited: Dec 1, 2022
  2. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    More information.
    After translating the spawned prefab, the corresponding AR Tracked Image appears to be destroyed!

    Code (CSharp):
    1. // Event Handler
    2.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    3.     {
    4.         foreach (var trackedImage in eventArgs.added)
    5.         {
    6.             //Debug.Log(trackedImage.referenceImage.name + " was added");
    7.             var imageName = trackedImage.referenceImage.name;
    8.             var imageTransform = trackedImage.transform;
    9.             InstantiateTrackedImagePrefab(imageName, imageTransform);
    10.         }
    11.  
    12.         foreach (var trackedImage in eventArgs.updated)
    13.         {
    14.             Debug.Log(trackedImage.referenceImage.name + "was updated");
    15.         }
    16.  
    17.         foreach (var trackedImage in eventArgs.removed)
    18.         {
    19.             // this doesnt actually work - the removed event never triggers
    20.             // Debug.Log(trackedImage.referenceImage.name + "was removed");
    21.         }
    22.     }
    when eventArgs.updated is called any tracked Image that had a translated prefab returns the following error in XCode

    NullReferenceException: Object reference not set to an instance of an object.
    at UnityEngine.Component.get_transform () [0x00000] in <00000000000000000000000000000000>:0


    at UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].SetSessionRelativeData (TTrackable trackable, TSessionRelativeData data) [0x00000] in <00000000000000000000000000000000>:0

    at UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].CreateOrUpdateTrackable (TSessionRelativeData sessionRelativeData) [0x00000] in <00000000000000000000000000000000>:0

    at UnityEngine.XR.ARFoundation.ARTrackableManager`5[TSubsystem,TSubsystemDescriptor,TProvider,TSessionRelativeData,TTrackable].Update () [0x00000] in <00000000000000000000000000000000>:0
     
  3. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    I looked into the AR Translation component code and saw where it destroys the old Anchor (parent game object) when the drag is complete. Since I am attaching my prefabs directly to the tracked images, this is where my problem occurs.

    I tried modifying my code to add in an empty GO "between" the tracked image's transform (parent) and my instantiated prefab (child) but as soon as I end my drag action this "middle man" is destroyed and I end up with the same problem that had before where the tracked image and the new prefab are no longer related. So the updated tracked image event leaves the prefabs orphaned in world space. At least my tracked images aren't being destroyed now
     
  4. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    OK I think I have this fixed! I just needed to add in some code to the eventArgs.updated loop.
    Basically I make sure that the prefab exists and if it does I manually reset it's position and rotation and re establish the hierarchy.
     
  5. om3l3tte

    om3l3tte

    Joined:
    Jun 8, 2021
    Posts:
    1
    Hey rsauchuck-gp, could you please share the code for how you did this? I'm having the same problem since I'm attaching some canvas stuff to the parent as well.
     
  6. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    In my initial code, on line 9, I call the function that attaches the prefab directly to the image's transform. This is why it breaks. Instead i need to add a "middleman" GO and instantiate the prefab on THAT instead

    Code (CSharp):
    1. var anchor = new GameObject("PlacementAnchor").transform;
    2. anchor.position = imageTransform.position;
    3. anchor.rotation = imageTransform.rotation;
    4. anchor.parent = imageTransform;
    5. InstantiateTrackedImagePrefab(imageName, anchor);
    THEN on line 14 in the .updated block I add in the following

    Code (CSharp):
    1.  var imageName = trackedImage.referenceImage.name;
    2. Debug.Log(imageName + "was updated");
    3. if (instantiatedPrefabs.ContainsKey(imageName))
    4.         {
    5.             instantiatedPrefabs[imageName].transform.parent.transform.position = trackedImage.transform.position;
    6.             instantiatedPrefabs[imageName].transform.parent.transform.rotation = trackedImage.transform.rotation;
    7.             instantiatedPrefabs[imageName].transform.parent.transform.parent = trackedImage.transform;
    8.  
    9.             }
    10.