"AR Foundation camera distance, object looses its position, but unity position is in place" (780463)

Simple case :
Simple AR scene,
You put a cube in a scene in front of you,
debug.log cube position and rotation, build and run,
After build object is in place, log reads its position its ok,
then you cover camera,
everything freezes, you rotate the device somewhere else then the object looses its position
debug.log prints that it is the same position and rotation, it seems that skybox itself rotates
when you cover up camera, how to know if an object has lost its position?
How to know if an object is in a different position or a
position is replaced if the position of the object is not really replaced in unity?
I need to notify users to come back to the initial position to see AR objects once again.
Thank you!!

Paul.

found it!
ARSession.cs

switch (subsystem.trackingState) {
case TrackingState.None:
case TrackingState.Limited:
Debug.Log (“NOT TRACKING!!!”);
state = ARSessionState.SessionInitializing;
break;
case TrackingState.Tracking:
state = ARSessionState.SessionTracking;
break;
}

But still cant find where i can log values when an object is relocated to a different position

1 Like

Someone??? Why gyro freezes when you cover camera???

Hi paulksi,

When you cover the camera, AR subsystem loses visual tracking and when you uncover it, system tries to relocalize itself. Whole world origin may change, but your object’s transform will be stay at the same previous position relative to the world origin. This is why you see the same positions in the log.

You should add an anchor when you place your object in AR. Anchored object can be relocalized most of time. Even if the world origin is changed due to the tracking state, you object will stay at the same position in real world.

See: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.1/manual/anchor-manager.html

Good luck!

1 Like

Thank you ibrahimpenekli !

Here is the setup - AR Anchor manager is on AR Session Origin,
Gameobject is that cube, i`m doing this right?

The result is still the same, attached youtube video

5609926--580717--upload_2020-3-20_10-26-24.png 5609926--580720--upload_2020-3-20_10-27-34.png

https://www.youtube.com/watch?v=VXja6Q9dxO4

You have to add or attach anchor via code I think:
https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.1/manual/anchor-manager.html

“To add or remove anchors, call AddAnchor or RemoveAnchor on the ARAnchorManager component in script.”

When you call AddAnchor, ARAnchorManager instantiates ARAnchor object for you. Then you can set your gameobject as child of that anchor. Or create a prefab and assign to ARAnchorManager. On awake, call AddAnchor with position and orientation you specified, it will crate your object automatically.

I hope this helps!

@paulksi did you manage to solve the issue?

Kinda

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.SceneManagement;

[RequireComponent (typeof (ARAnchorManager))]
[RequireComponent (typeof (ARRaycastManager))]
public class AnchorCreator : MonoBehaviour {

public Vector3 myVector;
public Quaternion myAngles;
public Pose hitPose;

public List s_Hits = new List ();

List m_Anchors;

ARRaycastManager m_RaycastManager;

ARAnchorManager m_AnchorManager;

public void RemoveAllAnchors () {
foreach (var anchor in m_Anchors) {
m_AnchorManager.RemoveAnchor (anchor);
}
m_Anchors.Clear ();
}

void Awake () {
m_RaycastManager = GetComponent ();
m_AnchorManager = GetComponent ();
m_Anchors = new List ();

myVector = new Vector3 (0.0f, 1.0f, 0.0f);
myAngles = Quaternion.Euler (0, 30, 0);
}

void Update () {

if (Input.touchCount == 0)
return;

var touch = Input.GetTouch (0);
if (touch.phase != TouchPhase.Began)
return;

if (m_RaycastManager.Raycast (touch.position, s_Hits, TrackableType.FeaturePoint)) {
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
// hitPose = s_Hits[0].pose;
// Debug.Log (“FIRST” + s_Hits[0].pose);
// s_Hits[0].pose = ((0.0, 0.5, 0.3), (0.0, 0.5, 0.3));
// Debug.Log (“SECOND” + s_Hits[0].pose);

// var anchor = m_AnchorManager.AddAnchor (hitPose);

}
}

public void AddElements () {

// hitPose = ((0.0f,0.0f,2.0f),(0.0f,0.0f,2.0f));

var anchor2 = m_AnchorManager.AddAnchor (hitPose);

if (anchor2 == null) {
Logger.Log (“Error creating anchor”);
} else {
m_Anchors.Add (anchor2);
}

}

public void ReloadScene () {

// hitPose = ((0.0f,0.0f,2.0f),(0.0f,0.0f,2.0f));

SceneManager.LoadScene(“Anchors”, LoadSceneMode.Single);

}

}

I am really not able to figure it out how I can use your code. My problem is I can able to place the object on AR plane but the moment I am walking little far from the placed object then I can see my object gets drift a bit, In other words we can say the object has changed its position. Could some one please help a bit. @paulksi @newguy123 @ibrahimpenekli @tdmowrer

I also share this drift problem, I only have one object in my scene. I have both a plane set on the ground, an anchor attached to it with the gameobject, and it’ll still drift : I’ll move back if I more IRL forward and front if I move back.

nothing is a child of my AR camera, and the anchor dosn’t drift if I rotate, it only on the horizontal plane

If plane detection is enabled in your scene, your AR Foundation provider will continue to update the size, position, and rotation of detected planes as the device scans more of your environment.

To statically anchor a plane to a specific point, I recommend first disabling plane detection after you have detected your desired plane. (To do this, disable the ARPlaneManager). Then reparent the ARPlane GameObject as a child of an ARAnchor GameObject.

1 Like