I'm developing an Android app using AR Foundation, and I'm experiencing an object drifting issue

I’m developing an Android app using AR Foundation, and I’m experiencing an object drifting issue.

my code

void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    {
        foreach (ARTrackedImage trackedImage in eventArgs.added)
        {
            CreateOrUpdateARObject(trackedImage);
        }

      foreach (ARTrackedImage trackedImage in eventArgs.updated)
      {
          CreateOrUpdateARObject(trackedImage);
      }

      foreach (ARTrackedImage trackedImage in eventArgs.removed)
      {
      }
  }

void CreateOrUpdateARObject(ARTrackedImage trackedImage)
{
    if (trackedImage.trackingState == TrackingState.Tracking && trackedImage.referenceImage.name == "8221")
    {
        if (!arObjectPrefab[0].activeSelf)
        {
            arObjectPrefab[1].SetActive(false);
            arObjectPrefab[2].SetActive(false);
            arObjectPrefab[3].SetActive(false);
            GameObject spawnedObject = arObjectPrefab[0];
            spawnedObject.SetActive(true);

            spawnedObject.transform.position = trackedImage.transform.position;
            spawnedObject.transform.rotation = Quaternion.identity;
            spawnedObject.transform.SetParent(trackedImage.transform);
        }
    }

Even when the object is pre-placed or instantiated, it always seems to follow the camera.

I also touched the plane through Plane Manager and created it at the touch position.

It didn’t work either, and the object always followed the camera even when it was done via AR raycast.

I’ve tried changing both the Unity version and the AR Foundation version to fix this issue, but the problem persists.

I encountered the same phenomenon with Unity 6.

Here are the Unity versions I tried:

2022.3.35f1
2022.3.6f1
2021.3.29f1
6preview
I installed the AR Foundation version according to the requirements.

I’ve been struggling with this for months. I need help.

2 Likes

Basically, Vuforia provides better Image Tracking than AR Foundation, but there is a trick for AR Foundation.

A common recommendation is to improve the Lighting in your Room in Real Life to avoid Shifting. Also, iOS here provides better Image Tracking than Android.


There is another trick:

  1. Scan AR Marker at starting point: AR Tracked Image Manager component | AR Foundation | 5.1.5
  2. Put AR Anchor here to avoid shifting during the session: AR Anchor Manager component | AR Foundation | 5.1.5

Resources to help you:

You should create an ARAnchor at the pose (position and rotation) of the tracked image and attach your content to the anchor. We recently updated our documentation on anchors to provide more information and context about using anchors and tracked images here Introduction to anchors | AR Foundation | 6.0.3.

1 Like
            trackedImage.AddComponent<ARAnchor>();

            GameObject spawnedObject = arObjectPrefab[0];
            spawnedObject.SetActive(true);

            spawnedObject.transform.position = trackedImage.transform.position;
            spawnedObject.transform.rotation = trackedImage.transform.rotation;
            spawnedObject.transform.SetParent(trackedImage.transform);

I’m sorry for the missing information. What I want to do is recognize an image, create objects, and allow navigation through the space, as if entering a room.

I tried like this. But the object is still drifting.

Is this what you mean?

1 Like

Is the image going to move and you want the objects to follow the image? If so then presumably the image will be in view the whole time and you can update your content to the position and rotation of the image without using an anchor.

If the image is stationary and wont always be in view of the camera then you should create a new anchor either by calling TryAddAnchorAsync on the anchor manager or by creating a new GameObject, setting the position of the new GameObject to the same as your tracked image and then add an ARAnchor component to it.

Something like this

GameObject anchor = new("Anchor");
anchor.transform.position = trackedImage.transform.position;
anchor.transform.rotation = trackedImage.transform.rotation;
anchor.AddComponent<ARAnchor>();

GameObject spawnedObject = arObjectPrefab[0];
spawnedObject.SetActive(true);
spawnedObject.transform.SetParent(anchor.transform);

You shouldn’t add the ARAnchor component directly to the tracked image because then you’ll have two subsystems trying to control the same trackable which would cause problems.

1 Like