Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PolarToGeographical : MonoBehaviour {
  5.  
  6.     public Transform planet;
  7.     float planetRadius {
  8.         get {
  9.             return planet.gameObject.GetComponent<SphereCollider>().radius;
  10.         }
  11.     }
  12.     Vector3 relPos {
  13.         get {
  14.             return this.transform.position - planet.position;
  15.         }
  16.     }
  17.  
  18.     struct Directionality {
  19.         public Vector3 up;
  20.         public Vector3 north;
  21.         public Vector3 east;
  22.         public Vector3 west {
  23.             get {
  24.                 return east * -1f;
  25.             }
  26.         }
  27.         public Vector3 south {
  28.             get {
  29.                 return north * -1f;
  30.             }
  31.         }
  32.         public Vector3 down {
  33.             get {
  34.                 return up * -1f;
  35.             }
  36.         }
  37.     }
  38.  
  39.     struct Geographical {
  40.         public float latitude;
  41.         public float longitude;
  42.         public float height;
  43.     }
  44.  
  45.     //returns current directionality
  46.     Directionality getDirectionality() {
  47.         Directionality direction = new Directionality ();
  48.         direction.up = relPos.normalized * -1f;
  49.         direction.east = Vector3.Cross (Vector3.up, direction.up).normalized;
  50.         direction.north = Vector3.Cross (direction.up, direction.east).normalized;
  51.  
  52.         return direction;
  53.     }
  54.  
  55.     Geographical getGeographical() {
  56.         Geographical geo = new Geographical ();
  57.         //Use a more complicated solution for vMod if the planet will move or rotate.
  58.             //goal is to project onto planet's xz-plane
  59.             //I recommend this transform's worldToLocalMatrix method
  60.             //docs.unity3d.com/ScriptReference/Transform-worldToLocalMatrix.html
  61.         Vector3 vMod = relPos;
  62.         vMod.y = 0f;
  63.  
  64.         geo.latitude = Vector3.Angle (vMod, relPos);
  65.         if (relPos.y < 0f)
  66.             geo.latitude *= -1f;
  67.  
  68.         geo.longitude = Vector3.Angle (vMod, planet.forward);
  69.         if (relPos.x < 0f)
  70.             geo.longitude *= -1f;
  71.  
  72.         geo.height = (relPos.magnitude - planetRadius) * -1f;
  73.  
  74.         return geo;
  75.     }
  76.  
  77.     void OnDrawGizmos() {
  78.         //unnecessary, but reads better
  79.         Vector3 pos = this.transform.position;
  80.         Directionality direction = getDirectionality ();
  81.  
  82.         Gizmos.color = Color.green;
  83.         Gizmos.DrawLine (pos, pos + direction.up);
  84.        
  85.        
  86.         Gizmos.color = Color.red;
  87.         Gizmos.DrawLine (pos, pos + direction.east);
  88.        
  89.         Gizmos.color = Color.blue;
  90.         Gizmos.DrawLine (pos, pos + direction.north);
  91.  
  92.  
  93.         Geographical geo = getGeographical ();
  94.         this.gameObject.name = (
  95.             "Lon: "
  96.             + geo.longitude.ToString("N")
  97.             + ", Lat: "
  98.             + geo.latitude.ToString("N")
  99.             + ". Ht: "
  100.             + geo.height.ToString("N")
  101.         );
  102.     }
  103. }