Latitudes and Longitudes in Unity

41 views Asked by At

I have an earth model and I want an object to move according to the latitudes and longitudes on the model. I have the formula for converting lats and longs to ECEF XYZ but it exceeds the floating -point precision in unity and my model is pretty small. I am using it because I wanted an offline map. Anyways, I am using this formula, I tried changing the radius to my model radius but it does not work

public const float EarthRadius = 6,371f;
{
    float cosLat = Mathf.Cos(latitude * Mathf.Deg2Rad);
    float sinLat = Mathf.Sin(latitude * Mathf.Deg2Rad);
    float cosLon = Mathf.Cos(longitude * Mathf.Deg2Rad);
    float sinLon = Mathf.Sin(longitude * Mathf.Deg2Rad);

    float radius = EarthRadius / Mathf.Sqrt(1.0f - 0.00669437999014f * sinLat * sinLon);

    float x = (radius + altitude) * cosLat * cosLon;
    float y = (radius + altitude) * cosLat * sinLon;
    float z = (radius * (1.0f - 0.00669437999014f) + altitude) * sinLat;

    return new Vector3(x, y, z);

}

How can I solve this conversion to get correct coordinates? It does not have to be precise. I just want my object to move to that latitudes and longitudes.

0

There are 0 answers