How to convert a compass bearing sensor reading to a direction mark (N,S,W,E)?

Hello forum. I need some assistance on making a bearing compass script snippet I have, to transform the sensor readings to a GUI.Label , as of N, W, S, E, etc.

I want on Android screen to show e.g. this message “You are heading N” when the bearing reading is e.g. “0” or “360”, and e.g. “You are heading NE” when the reading is between “0-25” degrees, etc.

Here is the script for compass bearing:

``````#pragma strict

var updateFreq = 0.1;
private var gpsRef : Transform;
private var gpsRefN : int;
private var timer = 0.0;

// Use this for initialization
function Start () {
Input.compass.enabled = true;
}
// Update is called once per frame
function Update () {

timer+=Time.deltaTime;
if(timer>(1/updateFreq))
{
timer=0;
}

// Orient an object to point northward.
}
``````

Can someone, please assist me on this, by posting a sample code or a tutorial on how to read bearing of compass heading and convert them to direction marks NSWE?
There is a video tutorial at this link, but it is in C, and I cannot code in this language (only JS-UnityScript and a little C#). Thank you all in advance for your answers.

I don’t understand… isn’t it just a bunch of “if” statements? Something like:

``````string compass = "?";
compass = "N";
else if (heading < 60) {
compass = "NE";
else if (heading < 120) {
compass = "E";
} // ...etc...
``````
1 Like

360 / 8 directions gives 45. Hence an array of 8 strings could be indexed

``````public class Heading
{
private string[] m_names[]=new string[8] { "N","NE","E","SE","S","SW","W","NW" };
public string this[float angle]{get{return m_names[((int)((angle-22.5f)/45.0f))&7]}}
}
``````

or

``````public enum HEADING {N, NE, E, SE, S, SW, W, NW}
``````

coupled with

``````string heading = System.Enum.GetName( typeof(HEADING), ((int)((angle-22.5f)/45.0f))&7);
``````

etc…

2 Likes

Hello @JoeStrout . It is indeed a matter of if statements. Sad, I didn’t think of it. Thank you for posting this sample code snippet. If I have to show these direction mark of the compass on a GUI.Label, how should I do it? I know how to do this with a GUI.Text, but I need to have it show on a GUI.Label if it is possible. Thank all again for your time and effort.

While they are discussing on how to align sucessions of IFs and how to display a text, i thought i would take advantage of the intermission and land a tiny sample project: https://www.sugarsync.com/pf/D2307301_98582733_622565
which allow to simulate a heading with a slide bar, while the linear and normal compasses respond to the heading you input:

@Exnihilon , just assign to the .text property of the GUI.Label.

Hi @Yseron . Nice project you got posted here. Need to ask though if it is possible to work without input by user. Should the script be always attached to main camera or it can work other ways ? Do you also need to add the accelerometer class, to get the movement of the device if it should be ported for a mobile?

I did simulate an input with a slider because holding my PC and shaking in all the directions does not seem to work well.
The script can be attached to whatever you want from the moment that it is a game object.
You of course need to add or write whatever class is interfacing with the inputs you would get from a device instead of a slider.