Search Unity

Question Adding AR Anchor to GameObject alters go's position.

Discussion in 'AR' started by KommaStra, Apr 29, 2021.

  1. KommaStra

    KommaStra

    Joined:
    Nov 12, 2019
    Posts:
    3
    Hello together,

    since yesterday I encounter a strange problem. When adding an ARAnchor component to my gameobject the position of the gameobject changes significantly.

    I am adding the gameobject via:

    Code (CSharp):
    1. public void positionPinAt(LeanFinger position)
    2.     {
    3.         var ray = Camera.main.ScreenPointToRay(position.ScreenPosition);
    4.         var hits = new List<ARRaycastHit>();
    5.         var hasHit = raycastManager.Raycast(ray, hits, raycastMask);
    6.         if (hasHit)
    7.         {
    8.             // Create an instance of the prefab
    9.             var _pin = Instantiate(pin, hits.First().pose.position, Quaternion.identity);
    10.  
    11.             // Add an ARAnchor component if it doesn't have one already.
    12.             if (_pin.GetComponent<ARAnchor>() == null)
    13.             {
    14.                 _pin.AddComponent<ARAnchor>();
    15.             }
    16.         }
    17.     }
    When stepping through the code I see that the position where the raycast hits the plane is calculated correctly. The object is being placed at the desired location until the ARAnchor component is beeing added. If I dont add the ARAnchor component everything works as expected. As soon as the anchor component is added, the position changes.

    Whats very suspicious to me is that the after attaching the anchor component, the position of the gameobejct is being changed approximatley (not exactly) to where the center of my canvas is in worldspace. This odd behaviour stays the same regardless of where I tap on the screen.

    Another odd thing is that this behaviour only occurs when using ARFoundationRemote, not when building the app.

    Also weird: the same behaviour occurs even if I add the ARAnchor component to a freshly created cube. Once the component is added, its position changes.


    Any help would be greatly appreciated.

    Thank you!
     

    Attached Files:

    Finn_Nua likes this.
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    Connected with the team about this. This is the expected behavior. Adding an ARAnchor tells AR Foundation that you wish to drive the game object's position based on the AR data for where that position is tracked in space.
     
  3. Finn_Nua

    Finn_Nua

    Joined:
    Mar 27, 2023
    Posts:
    9

    I am also having the same problem.

    I am using a tracked image manager to recognise an image and instantiate a prefab at that point. The prefab briefly appears at the desired location (in front of the image) but once the anchor is attached, the prefab jumps to the point where the app was first started rather than where the image actually is.

    if this is expected behaviour, could you elaborate on how to make sure the anchor position is the same as (in my case) the image's position rather than the (0,0,0) of when the app was "turned on"

    I've attached an image in case I'm not describing this well

    Thanks!
     

    Attached Files:

  4. Finn_Nua

    Finn_Nua

    Joined:
    Mar 27, 2023
    Posts:
    9


    I'm having the same problem. Did you ever find a solution to this?