title: Realistic GPS sensor as a Unity Game object
body: <p>I'm trying to build a Global Positioning System sensor script(not the one that uses the real life Global Positioning System sensor), and use it as a game object. I'm trying to implement this sensor in a self driving car project. I have tried writing my own script which converts the x, y, z coordinates into longitude and latitude, What i would like to understand is just like building a radar sensor and light detection and ranging sensor and Inertial measurement unit sensor, how can i build a Global Positioning System sensor which gives us coordinates which accurately measures the position of one game object with respect to another game object withing the scene(world).</p>
  
  <p>this is the script i have tried to build</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System;
  //using System.Math;
  //using System.Diagnostics;
  
  
  public class GPS02 : MonoBehaviour {
  
         public float latitude;
         public float longitude;
         public double earthradius;
  
         public double Lat_lon;
  
         public float lat;
         public float lon;
  
  
         public static double enu2ecefx;
         public static double enu2ecefy;
         public static double enu2ecefz;
  
         public static float finalX;
         public static float finalY;
         public static float finalZ;
  
         const double a = 6378137f;         // WGS-84 Earth semimajor axis (m)
  
         const double b = 6356752f;     // Derived Earth semiminor axis (m)
         const double f = (a - b) / a;           // Ellipsoid Flatness
         const double f_inv = 1.0f / f; 
         const double a_sq = a * a;
         const double b_sq = b * b;
         const double e_sq = f * (2f - f);
  
  
        public GameObject carvar;
  
        public Car othervar;
  
        public double E;
        public double N;
        public double U;
  
      // Use this for initialization
      void Start () {
           othervar = carvar.GetComponent&lt;Car&gt;();
           E = othervar.east;
           N = othervar.north;
           U = othervar.up;       
           transform.position = Quaternion.AngleAxis(longitude, -Vector3.up) * Quaternion.AngleAxis(latitude, -Vector3.right) * new Vector3(0,0,1);  // ----not sure
          Debug.Log("X ECEF "+transform.position.z);
          Debug.Log("Y ECEF "+transform.position.x);
          Debug.Log("Z ECEF "+transform.position.y);
  
          EnuToEcef(E,N,U,latitude,longitude,earthradius,out enu2ecefx,out enu2ecefy,out enu2ecefz);
  
         finalX = transform.position.z + (float)enu2ecefx;
         finalY = transform.position.x + (float)enu2ecefy;
         finalZ = transform.position.y + (float)enu2ecefz;
  
          Debug.Log("final X "+finalX);
          Debug.Log("final Y "+finalY);
          Debug.Log("final Z "+finalZ);
  
          lat = (float)Math.Acos( finalY / earthradius);
          lon = (float)Math.Atan(finalX/ earthradius); 
  
          Debug.Log("lattitude "+lat);
          Debug.Log("lattitude "+lon);
      }
  
      // Update is called once per frame
      void Update () {
  
      }
  
  
      public  static void EnuToEcef(double E, double N, double U,
                                  double latitude, double longitude, double earthradius,
                                  out double enu2ecefx, out double enu2ecefy, out double enu2ecefz)
      {
          // Convert to radians in notation consistent with the paper:
          var lambda = DegreeToRadians(latitude);
          var phi = DegreeToRadians(longitude);
          var s = Math.Sin(lambda);
          var n = a / Math.Sqrt(1 - e_sq * s * s);
  
          var sin_lambda = Math.Sin(lambda);
          var cos_lambda = Math.Cos(lambda);
          var cos_phi = Math.Cos(phi);
          var sin_phi = Math.Sin(phi);
  
          double x0 =(earthradius + n) * cos_lambda * cos_phi;
          double y0 = (earthradius + n) * cos_lambda * sin_phi;
          double z0 = (earthradius + (1 - e_sq) * n) * sin_lambda;
  
          double xd = -sin_phi * E - cos_phi * sin_lambda * N+ cos_lambda * cos_phi * U;
          double yd = cos_phi * E - sin_lambda * sin_phi * N + cos_lambda * sin_phi * U;
          double zd = cos_lambda * N + sin_lambda * U;
  
          enu2ecefx = xd + x0;
          enu2ecefy = yd + y0;
          enu2ecefz = zd + z0;
  
          Debug.Log("car_x ECEF "+enu2ecefx);
          Debug.Log("car_y ECEF "+enu2ecefy);
          Debug.Log("car_z ECEF "+enu2ecefz);
      }
  
      public static double DegreeToRadians(double Lat_lon)
      {
          return (3.14f/180f)*Lat_lon;
      }
  }
  </code></pre>