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

Question AR Foundation ARKIT gameobjects placed on mesh drift

Discussion in 'AR' started by ibompuis, Aug 31, 2021.

  1. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    100
    Hi,
    I use AR foundation and ARKIT and IPAD with LIDAR on unity 2020.3.13f1.
    How to prevent drift of gameobjects on the mesh.
    First image I instantiated some sphere and continue to scan the place. After going back to my initial position sphere drifted a lot. Why this is moving ?



    the update function:
    Code (CSharp):
    1. if(isActivate)
    2.         {          
    3.             if(Input.touchCount > 0)
    4.             {
    5.                 Touch touch = Input.GetTouch(0);
    6.            
    7.                 if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    8.                 {
    9.                     Debug.Log("Touched the UI");
    10.                     return;
    11.                 }
    12.            
    13.                 touchPosition = touch.position;
    14.  
    15.                 if(touch.phase == TouchPhase.Began)
    16.                 {
    17.                     Ray ray = origin.camera.ScreenPointToRay(touch.position);
    18.                     RaycastHit hitObject;
    19.                     if(Physics.Raycast(ray, out var hit, float.PositiveInfinity, 1 << meshManager.meshPrefab.gameObject.layer))
    20.                     {
    21.                         lastSelectedObject = hit.transform.GetComponent<PlacementObject>();
    22.                         if(lastSelectedObject != null)
    23.                         {
    24.                             PlacementObject[] allOtherObjects = FindObjectsOfType<PlacementObject>();
    25.                             foreach(PlacementObject placementObject in allOtherObjects)
    26.                             {
    27.                                 placementObject.Selected = placementObject == lastSelectedObject;
    28.                             }
    29.                         }
    30.                     }
    31.                 }
    32.  
    33.                 if(touch.phase == TouchPhase.Ended)
    34.                 {
    35.                     lastSelectedObject.Selected = false;
    36.                 }
    37.             }
    38.  
    39.             for (int i = 0; i < Input.touchCount; i++)
    40.             {
    41.                 var touch = Input.GetTouch(i);
    42.                 var ray = origin.camera.ScreenPointToRay(touch.position);
    43.                 var hasHit = Physics.Raycast(ray, out var hit, float.PositiveInfinity, 1 << meshManager.meshPrefab.gameObject.layer);
    44.                 if (hasHit)
    45.                 {
    46.                     if(lastSelectedObject == null)
    47.                     {
    48.                         lastSelectedObject = Instantiate(placedPrefab, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)).GetComponent<PlacementObject>();
    49.                        
    50.                         points.Add(lastSelectedObject.transform);
    51.                         line.SetupLine(points);
    52.                     }
    53.                     else
    54.                     {
    55.                         if(lastSelectedObject.Selected)
    56.                         {
    57.                             lastSelectedObject.transform.position = hit.point;
    58.                             lastSelectedObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    59.                         }
    60.                     }
    61.                 }
    62.             }
    63.         }
    Best,
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    You can use AR Anchors to keep the objects in place. :)
     
  3. ibompuis

    ibompuis

    Joined:
    Sep 13, 2012
    Posts:
    100
    Yeah I thought about it, does AR Anchors works with mesh manager ?
     
  4. crazyrems

    crazyrems

    Joined:
    Feb 25, 2013
    Posts:
    5
    Hi,

    I’m also in charge of the development of this app.

    So my understanding of Anchor points are they make virtual objects move when either feature points or planes moves. Are they binded to the ARKit ARAnchor system?

    I tried different things with the Unity ARFoundation demo project, mostly with the Anchor demo.
    I ended with the same issue by reproducing the movements done as described in the previous post, both the plane based and the feature point based anchors drifted from their original positions.

    IMG_0010.PNG IMG_0011.PNG

    I guess that that’s a limitation we can’t completely overcome. We need to be cautious about the kind of movements we make during the process by considering safer tracking patterns and the way we hold the device. I am trying to define the kind of movements to adopt in order to reduce all those issues.

    What are the additional tips you can give?
     
    Last edited: Sep 14, 2021
  5. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324