Created
October 29, 2020 18:54
-
-
Save helloCaller/1a4be6b6ebffa9401fa527f824d913ee to your computer and use it in GitHub Desktop.
A Unity compass script to point in app objects towards a real world location
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using TMPro; | |
using System; | |
using UnityEngine.SceneManagement; | |
public class CompassBehaviour : MonoBehaviour { | |
//script for having an object point towards an IRL point in space from within the app using phone location and orientation. | |
private bool startTracking = false; | |
//holds location of phone (location services must be accessed and permissions asked) | |
private float currentLatitude; | |
private float currentLongitude; | |
//set destination long and lat here | |
private float destinationLatitude = 43.6389881f; | |
private float destinationLongitude = -79.3821859f; | |
//calculate compass bearing from one location to another | |
private float angleFromCoordinate(float lat1, float long1, float lat2, float long2) | |
{ | |
lat1 *= Mathf.Deg2Rad; | |
lat2 *= Mathf.Deg2Rad; | |
long1 *= Mathf.Deg2Rad; | |
long2 *= Mathf.Deg2Rad; | |
float dLon = (long2 - long1); | |
float y = Mathf.Sin(dLon) * Mathf.Cos(lat2); | |
float x = (Mathf.Cos(lat1) * Mathf.Sin(lat2)) - (Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon)); | |
float brng = Mathf.Atan2(y, x); | |
brng = Mathf.Rad2Deg * brng; | |
brng = (brng + 360) % 360; | |
brng = 360 - brng; | |
return brng; | |
} | |
void Start() { | |
Input.compass.enabled = true; | |
Input.location.Start(); | |
StartCoroutine(InitializeCompass()); | |
} | |
void Update() { | |
if (startTracking) { | |
currentLatitude = Input.location.lastData.latitude; | |
currentLongitude = Input.location.lastData.longitude; | |
float bearing = angleFromCoordinate(currentLatitude, currentLongitude, destinationLatitude, destinationLongitude); | |
//smooth the rotation as it continually updates | |
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, Input.compass.magneticHeading + bearing), 100f); | |
} | |
} | |
IEnumerator InitializeCompass() { | |
yield return new WaitForSeconds(1f); | |
startTracking |= Input.compass.enabled; | |
} | |
//function to calculate distance to destination | |
private float currentDistanceToDestination(float currentLat, float currentLong, float destinationLat, float destinationLong){ | |
float latSquare = Mathf.Pow((destinationLat - currentLat), 2); | |
float longSquare = Mathf.Pow((destinationLong - currentLong), 2); | |
return Mathf.Sqrt(latSquare + longSquare); | |
} | |
//function to return true or false depending on distance to destination and a given allowance/accuracy. | |
private bool arrivedAtDestination (float dist, float allowance) { | |
if (dist <= allowance) { | |
return true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment