Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AR Foundation camera distance, object looses its position, but unity position is in place

Discussion in 'AR' started by paulksi, Mar 12, 2020.

  1. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    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.

     
    Last edited: Mar 13, 2020
  2. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    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;
    }
     
  3. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    But still cant find where i can log values when an object is relocated to a different position
     
  4. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    Someone????? Why gyro freezes when you cover camera????
     
  5. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    30
    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!
     
    paulksi likes this.
  6. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    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

    upload_2020-3-20_10-26-24.png upload_2020-3-20_10-27-34.png

     
  7. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    30
    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!
     
  8. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    1,248
    @paulksi did you manage to solve the issue?
     
  9. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    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<ARRaycastHit> s_Hits = new List<ARRaycastHit> ();

    List<ARAnchor> 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<ARRaycastManager> ();
    m_AnchorManager = GetComponent<ARAnchorManager> ();
    m_Anchors = new List<ARAnchor> ();

    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);

    }

    }
     
  10. vinay_vidhani

    vinay_vidhani

    Joined:
    Oct 27, 2016
    Posts:
    10
    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
     
    Last edited: Mar 29, 2021
  11. ScareFire200

    ScareFire200

    Joined:
    May 16, 2020
    Posts:
    4
    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
     
  12. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,025
    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.
     
    newguy123 likes this.