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): private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) { foreach (var trackedImage in eventArgs.added) { // newPrefab is a symbol which appears on the image so that i know if the image is detected string imageName = trackedImage.referenceImage.name; var newPrefab = Instantiate(ArPrefabs[0], trackedImage.transform); _instantiatedPrefabs[imageName] = newPrefab; //refi is my reference, from which i place every other element in the space var refi = Instantiate(rond,trackedImage.transform); //that's the part which create problem, without i have a bad tracking, but whith it, the refi teleport to my cellphone position if (refi.GetComponent<ARAnchor>() == null) { refi.AddComponent<ARAnchor>(); } //After that, i place every other element relatively to refi initialisation = true; ImageRef.SetActive(false); foreach (KeyValuePair<string, int[]> butt in bouton_pos) { bouton[butt.Key] = Instantiate(rond, refi.transform); 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]); TextMeshProUGUI texte = bouton[butt.Key].GetComponentsInChildren<TextMeshProUGUI>()[0]; if (butt.Key.Contains("B")) { texte.text = $"Presser {butt.Key}"; } else if (butt.Key.Contains("S")) { texte.text = $"Tourner {butt.Key}"; } else { texte.text = $"Vérifier {butt.Key}"; } bouton[butt.Key].SetActive(false); } } foreach (var trackedImage in eventArgs.updated) { //Allow me to make newPrefab dissapear when the image is not visible anymore _instantiatedPrefabs[trackedImage.referenceImage.name].SetActive(trackedImage.trackingState == TrackingState.Tracking); } foreach (var trackedImage in eventArgs.removed) { // it never go on this case Destroy(_instantiatedPrefabs[trackedImage.referenceImage.name]); Debug.Log("destroy"); _instantiatedPrefabs.Remove(trackedImage.referenceImage.name); } } }
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
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: Currently you keep the anchor anchored to the image. You might not want to keep the custom anchor as child of the image trackable. 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.