Search Unity

Question image tracking and Anchor

Discussion in 'AR' started by Hailouu, May 8, 2023.

  1. Hailouu

    Hailouu

    Joined:
    Oct 19, 2022
    Posts:
    1
    Hello, I'm trying to create a code to place elements in space from a QR code. When the QR code is detected, prefabs must be placed relative to it. However, when I go more than 70cm away from the QR code, the image is no longer detected, which results in seeing my elements not in the right place.

    I had the idea of putting an anchor at the position of the image, then placing my elements relative to this anchor rather than relative to the tracked.image.transform to improve tracking. I tried with the following code, the anchor (refi) is placed well at the beginning however after 2/3 seconds, the prefab refi contenting the anchor teleports to the position of my phone in space (and with its orientation) . I have no idea why.

    thank you in advance for your help
    Code (CSharp):
    1. private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    2.     {
    3.  
    4.  
    5.         foreach (var trackedImage in eventArgs.added)
    6.         {
    7.             // newPrefab is a symbol which appears on the image so that i know if the image is detected
    8.             string imageName = trackedImage.referenceImage.name;
    9.             var newPrefab = Instantiate(ArPrefabs[0], trackedImage.transform);
    10.             _instantiatedPrefabs[imageName] = newPrefab;
    11.  
    12.  
    13.             //refi is my reference, from which i place every other element in the space
    14.             var refi = Instantiate(rond,trackedImage.transform);
    15.            
    16.  
    17.             //that's the part which create problem, without i have a bad tracking, but whith it, the refi teleport to my cellphone position
    18.             if (refi.GetComponent<ARAnchor>() == null)
    19.             {
    20.                 refi.AddComponent<ARAnchor>();
    21.             }
    22.            
    23.  
    24.  
    25.             //After that, i place every other element relatively to refi
    26.             initialisation = true;
    27.             ImageRef.SetActive(false);
    28.             foreach (KeyValuePair<string, int[]> butt in bouton_pos)
    29.             {
    30.                 bouton[butt.Key] = Instantiate(rond, refi.transform);
    31.                 bouton[butt.Key].transform.Translate(coord_ref[0] + 0.1f * butt.Value[1], coord_ref[1] - 0.5f, coord_ref[2] - 0.1f * butt.Value[0]);
    32.                 TextMeshProUGUI texte = bouton[butt.Key].GetComponentsInChildren<TextMeshProUGUI>()[0];
    33.  
    34.  
    35.                 if (butt.Key.Contains("B"))
    36.                 {
    37.                     texte.text = $"Presser {butt.Key}";
    38.                 }
    39.                 else if (butt.Key.Contains("S"))
    40.                 {
    41.                     texte.text = $"Tourner {butt.Key}";
    42.                 }
    43.                 else
    44.                 {
    45.                     texte.text = $"Vérifier {butt.Key}";
    46.                 }
    47.  
    48.                 bouton[butt.Key].SetActive(false);
    49.  
    50.             }
    51.  
    52.  
    53.  
    54.         }
    55.  
    56.         foreach (var trackedImage in eventArgs.updated)
    57.         {
    58.             //Allow me to make newPrefab dissapear when the image is not visible anymore
    59.             _instantiatedPrefabs[trackedImage.referenceImage.name].SetActive(trackedImage.trackingState == TrackingState.Tracking);
    60.         }
    61.  
    62.  
    63.         foreach (var trackedImage in eventArgs.removed)
    64.         {
    65.             // it never go on this case
    66.             Destroy(_instantiatedPrefabs[trackedImage.referenceImage.name]);
    67.             Debug.Log("destroy");
    68.             _instantiatedPrefabs.Remove(trackedImage.referenceImage.name);
    69.  
    70.         }
    71.  
    72.     }
    73. }
     
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
    We can't really help debug your code, but you have the right idea! It seems to me like your transform.Translate call might be doing this, but you can add some Debug.Log statements to your code to help you figure it out!

    You can also attach an IDE debugger to your running app: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html
     
  3. Dan4x8

    Dan4x8

    Joined:
    Feb 27, 2015
    Posts:
    13
    I second that. The idea is correct - we do essentially the same and it works like a charm.
    There are two important things you are missing:
    1. Currently you keep the anchor anchored to the image.
      You might not want to keep the custom anchor as child of the image trackable.
    2. Keep in mind, that it might take a frame or two until the pose of the image anchor is correct.
      Meaning, that if you unparent the anchor in the same frame as added, it most likely will have a completly wrong pose.
     
    andyb-unity likes this.