Created
September 27, 2013 18:02
-
-
Save keiranlovett/6732549 to your computer and use it in GitHub Desktop.
The compass script allows you to get 0-360 degree heading with respect to an arbitrary North reference object named "GPS Reference" of which the Z axis is pointed North. The compass uses a Sender to send the heading over a network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var updateFreq = 1.0; | |
private var heading = 0.0; | |
private var gpsRef : Transform; | |
private var gpsRefN : int; | |
private var sender : Sender; | |
private var timer = 0.0; | |
function Start(){ | |
Init(); | |
} | |
function Update () { | |
timer+=Time.deltaTime; | |
heading = transform.rotation.eulerAngles.y - gpsRefN; | |
if(heading<0)heading+=360; | |
if(timer>(1/updateFreq)){ | |
sender.Send(heading); | |
timer=0; | |
} | |
// Debug.Log(transform.rotation.eulerAngles.y + " - " + gpsRefN + " = " + heading); | |
} | |
function OnEnable(){ | |
Init(); | |
} | |
function Init(){ | |
var gpsRefGO = GameObject.Find("GPS Reference"); | |
if(gpsRefGO!=null){ | |
gpsRef = gpsRefGO.transform; | |
gpsRefN = gpsRef.rotation.eulerAngles.y; | |
sender = gameObject.GetComponent("Sender"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment