Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question ARCore positioning Objects and setting rotation

Discussion in 'AR' started by levoxtrip, Aug 17, 2023.

  1. levoxtrip

    levoxtrip

    Joined:
    May 4, 2020
    Posts:
    48
    Hey everyone, for an Android application I want to implement AR Image Detection and if the image is detected I need STABLE positioning and rotation. Right now its super buggy.
    I have on my prefabs anchors and in my scene the anchor manager. When the image gets detected the prefab is there but everytime with a different rotation.
    https://imgur.com/a/lNSpuCW
    https://imgur.com/a/zJAl3p2

    Here is my code:
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         _arTrackedImageManager = GetComponent<ARTrackedImageManager>();
    4.         foreach (GameObject arObject in arObjectsToPlace)
    5.         {
    6.  
    7.             GameObject newARObject = Instantiate(arObject, Vector3.zero, Quaternion.identity);
    8.                newARObject.name = arObject.name;
    9.             newARObject.SetActive(false);
    10.  
    11.             spawnedARObjects.Add(newARObject.name, newARObject);
    12.  
    13.         }
    14.     }
    15.  
    16.     private void Start()
    17.     {
    18.         animated = false;
    19.         UpdateFlowStateBtn.SetActive(false);
    20.     }
    21.  
    22.     public void OnEnable()
    23.     {
    24.         //subscribe to event of image detected
    25.         _arTrackedImageManager.trackedImagesChanged += OnImageChanged;
    26.     }
    27.  
    28.     public void OnDisable()
    29.     {
    30.         _arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
    31.     }
    32.  
    33.  
    34.     public void OnImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
    35.     {
    36.  
    37.         foreach (ARTrackedImage trackedImage in eventArgs.added)
    38.         {
    39.  
    40.             UpdateARImage(trackedImage);
    41.         }
    42.  
    43.         foreach (ARTrackedImage trackedImage in eventArgs.updated)
    44.         {
    45.             UpdateARImage(trackedImage);
    46.         }
    47.  
    48.         foreach (ARTrackedImage trackedImage in eventArgs.removed)
    49.         {
    50.             spawnedARObjects[trackedImage.name].SetActive(false);
    51.         }
    52.  
    53.     }
    54.  
    55.     private void UpdateARImage(ARTrackedImage trackedImage)
    56.     {
    57.         AssignGameObject(trackedImage.referenceImage.name, trackedImage.transform.localPosition, trackedImage.transform.rotation);
    58.     }
    59.  
    60.  
    61.  
    62.     private void AssignGameObject(string name, Vector3 newPosition, Quaternion rotationImage)
    63.     {
    64.  
    65.  
    66.         if (arObjectsToPlace != null)
    67.         {
    68.  
    69.          
    70.  
    71.             spawnedARObjects[name].transform.position = newPosition;
    72.  
    73.             spawnedARObjects[name].transform.rotation = rotationImage;
    74.            
    75.  
    76.             if (!spawnedARObjects[name].activeSelf) spawnedARObjects[name].SetActive(true);
    77.             if (!animated)
    78.             {
    79.            
    80.                 StartCoroutine(AnimationARObject(spawnedARObjects[name]));
    81.             }
    82.         }
    83.  
    84.  
    85.  
    86.     }
    87.  
    88.  
    89.     private IEnumerator AnimationARObject(GameObject AROb)
    90.     {
    91.         yield return new WaitForSeconds(0.1f);
    92.  
    93.         if (AROb.name == "ARFahndungsFoto")
    94.         {
    95.             AROb.LeanScale(scaleFactor * .8f, 2f).setEaseOutCubic().setOnComplete(ShowBtnUpdateFlow);
    96.             //AROb.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
    97.         }
    98.         else if (AROb.name == "ARZelt")
    99.         {
    100.             AROb.LeanScale(Vector3.one * .8f, 2f).setEaseOutCubic().setOnComplete(ShowARZeltUI);
    101.  
    102.         }
    103.  
    104.         else if (AROb.name == "ARTonband")
    105.         {
    106.             AROb.LeanScale(scaleFactor, 2f).setEaseOutCubic().setOnComplete(ShowARTonBand);
    107.         }
    108.         animated = true;
    109.     }
    Thanks in advance. Appreciate it
     
  2. thomas_key

    thomas_key

    Unity Technologies

    Joined:
    Dec 14, 2019
    Posts:
    35
    Repeating my reply to your post from the other thread:

    One thing to note that has come up a few times on the forums: Your lighting conditions matter greatly for any kind of tracking, especially on ARCore. Android devices currently don't have access to LIDAR like ARKit tracking does. So make sure the area you are working / testing in is well-lit or the images may have trouble being tracked/recognized.

    You may also want to look at using the arcoreimg to validate how recognizable your images are.

    A good reference for the ARCore image tracking specific quirks is the ARCore Augmented Images docs.
     
  3. thomas_key

    thomas_key

    Unity Technologies

    Joined:
    Dec 14, 2019
    Posts:
    35
    Also, looking at your script, you're assigning the localPosition/localRotation to the position field of your UI object.

    Instead try parenting your UI objects to the spawned trackable and manipulate your UI object's local transform values instead.
     
  4. levoxtrip

    levoxtrip

    Joined:
    May 4, 2020
    Posts:
    48
    Hey can you evaluate that a bit more please, what do you mean with parenting it to the spawned trackable? How would I do that?
     
  5. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    835