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.
  2. Dismiss Notice

Check if compass sensor available?

Discussion in 'Scripting' started by izeko2004, Nov 13, 2016.

  1. izeko2004

    izeko2004

    Joined:
    Dec 28, 2014
    Posts:
    74
    Hi,
    How can I check to see if the device has a compass?

    Thanks
     
  2. flatworldstudio

    flatworldstudio

    Joined:
    Jan 25, 2016
    Posts:
    2
    This seems to work: Try setting and reading Input.compass.enabled. If you enable it but it still read false, there's no compass.
     
  3. izeko2004

    izeko2004

    Joined:
    Dec 28, 2014
    Posts:
    74
    Thanks, will give this a go
     
  4. archriss

    archriss

    Joined:
    Jan 2, 2019
    Posts:
    1
    Reading Input.compass.enabled is not working for me (at least not on my iPhone).

    I'm not sure how accurate this is, but here's what I did:

    Code (CSharp):
    1. private IEnumerator IsCompassAvailable () {
    2.     bool compass = false;
    3.  
    4.     for (int i = 0; i < 10; i++) {
    5.         if (Input.compass.trueHeading != 0) {
    6.             compass = true;
    7.         }
    8.  
    9.         yield return new WaitForSeconds(0.05f);
    10.     }
    11.        
    12.     yield return compass;
    13. }
    Basically I take 10 samples of the sensor across 500ms, if they are all equal to 0 I assume that there is no compass available (this probably works with magneticHeading too).
    Obviously you need to enable the compass beforehand.

    I hope this helps someone!