Search Unity

Unity's Compass module giving very inaccurate results.

Discussion in 'AR' started by Overature, Jul 17, 2020.

  1. Overature

    Overature

    Joined:
    May 10, 2020
    Posts:
    25
    Hi,

    I am using Unity's Compass module to align a real time map in AR to North, so that it will line up in the real world. However while it works it is giving me very inconsistent results. Every time I open it it points somewhere new. The code I am using is below:
    Code (CSharp):
    1. public Transform target;
    2.  
    3.     public void Start()
    4.     {
    5.      
    6.         Input.compass.enabled = true;
    7.         Input.location.Start();
    8.      
    9.         target.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);
    10.     }
    11.  
    12.  
    Where "target" is the transform of the map object.

    Any help would be great!
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,145
    Hi!

    Setting the rotation of your AR object is not enough to align its orientation with real-world orientation.
    Every time you start your AR Session, the AR subsystem coordinates (and orientation) will be different. ARSessionOrigin has a specific method for this kind of situations:
    FindObjectOfType<ARSessionOrigin>().MakeContentAppearAt(targetTransform, position, rotation);

    Calling this method will not change the actual position and rotation of your target object, but will make it APPEAR to be placed at the desired position and orientation.
     
    Overature likes this.
  3. Overature

    Overature

    Joined:
    May 10, 2020
    Posts:
    25
    Hi,

    Thanks for the help, I got it to work for what I wanted! I need to do a bit of work to iron out the jittereryness of the compass. For anyone else here is the code I used using AR Foundation where target is the object to be aligned with North:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. namespace UnityEngine.XR.ARFoundation
    7. {
    8.     using System.Collections;
    9.     using System.Collections.Generic;
    10.     using System.Security.Cryptography;
    11.     using UnityEngine;
    12.     using UnityEngine.UI;
    13.  
    14.  
    15.     public class Compass : MonoBehaviour
    16.     {
    17.         private GameObject aRSessionOrigin;
    18.        
    19.         public GameObject target;
    20.         public Transform targetTransform;
    21.         private GameObject sign2;
    22.  
    23.         // Start is called before the first frame update
    24.         void Start()
    25.         {
    26.             Input.compass.enabled = true;
    27.             Input.location.Start();
    28.  
    29.  
    30.             aRSessionOrigin = GameObject.Find("AR Session Origin");
    31.             var aRScript = aRSessionOrigin.GetComponent<ARSessionOrigin>();
    32.  
    33.             aRScript.MakeContentAppearAt(targetTransform, targetTransform.position, Quaternion.Euler(0, -Input.compass.trueHeading, 0));
    34.  
    35.             //aRSessionOrigin.MakeContentAppearAt(target, target.position, Quaternion.Euler(0, -Input.compass.trueHeading, 0));
    36.  
    37.  
    38.             //FindObjectOfType<ARSessionOrigin>().MakeContentAppearAt(target, target.position, Quaternion.Euler(0, -Input.compass.trueHeading, 0));
    39.         }
    40.  
    41.         // Update is called once per frame
    42.         void Update()
    43.         {
    44.             aRSessionOrigin = GameObject.Find("AR Session Origin");
    45.             var aRScript = aRSessionOrigin.GetComponent<ARSessionOrigin>();
    46.             aRScript.MakeContentAppearAt(targetTransform, targetTransform.position, Quaternion.Euler(0, -Input.compass.trueHeading, 0));
    47.  
    48.             sign2 = GameObject.Find("Text2");
    49.             var sign2text = sign2.GetComponent<Text>();
    50.  
    51.             sign2text.text = Input.compass.trueHeading.ToString();
    52.  
    53.         }
    54.     }
    55. }