Lat Long to Unity world coordinates

Hi all,

I need to convert from (lat, long) to Unity 3D world coordinates to add some pins to my model. My model has the scale 1:1, 1 meter to 1 unity unit.
Hi have the (lat, long) of the (0,0,0) that is the reference.

Does anyone has a formula that can do the “magic”? I already did a research on the web, but probably using the wrong keywords.

I’m not an expert in Unity, and I know that this is a Math question, but until know I can’t found a solution. I’m trying to help my team mates on this. Trying to figure a formula that translates the lat, long to unity 3D coordinates.

Thanks in advance.

There might be some helper class someone has created out there somewhere, but there’s nothing built into Unity for this.

You’ve done the first part which is to determine what unit you want in the real world to match in Unity, but remember you are going to be limited by the resolution of a float, and changes in latitude and longitude are very very small over long distances.

The method you choose will also depend on which coordinate system you use for your LLA (latitude/longitude/altitude). You can use flat earth which assumes no earth curvature, which is usually fine for showing a map in small distances. You can use round earth which is simply using trigonometry to calculate the latitude and the longitude while assuming the altitude is 0 at sea level. I believe earth curvature is C = (x^2 + y^2)/2R where x and y are the distances (meters) in lat and long from point A to point B and R is the earth’s radius (meters). The equation assumes the altitude at point A matches the altitude at point B but C less height, so earth curvature might be 15 feet over 10 miles (just making that up), so if you wanted to draw a straight line from point A to point B, you’d need to account for that curve in the earth. Another round earth set of equations is the Haversine which are a bit more accurate than simple spherical earth equations.

Or you can use WGS-84 (I’d recommend) which is an ellipsoidal model but a bit more complicated mathematically. However, the equations are all out there, and you don’t have to consider earth curvature separately. It’s part of the equation, so you can easily convert between what’s called Earth Centered Earth Fixed (ECEF) to WGS-84. ECEF would basically align with your Unity universe, but the components are forward, right, down (FRD), so X is forward, Y is right, D is down, which means X in ECEF is really Z in unity, Y in ECEF is X in unity, and Z in ECEF is really -Y in Unity. I hope some of this makes sense…

There are tons of other coordinate systems out there as well that have their pros and cons for estimating position on the Earth. Obviously, with mountain ranges and valleys, you can’t model it perfectly without specifically having a discrete value for altitude at a set point across the globe, which is basically a large database. They exist, called Digital Terrain Elevation Data (DTED) maps. They come in various resolutions like 1 meter, 3 meter, etc, and there are other variations on them as well.

The bottom line is this is a huge subject, so you’ll need to get to know more about moving between earth coordinate systems and what’s available before you’ll be able to solve the problem and fully understand it.

If you just plan to display a big sphere and not be able to zoom really far in on it, you probably don’t need the resolution, and the Round Earth coordinate system makes the most sense. In that case, the ECEF value for a given latitude is just based on the angle. Remember, latitude is just the angle from the equator to the top or bottom of the earth, and it’s a constant change in distance over the sphere which is really just the distance along the arc at the surface of the earth while longitude is the angle from the prime meridian, but the distance between any two longitudes is not constant between any varying latitudes. In other words, the distance from lon of 10 deg to 11 deg at lat of 0 deg is not the same distance from 10 deg to 11 deg at a lat of 1 deg, etc. Because as your latitude increases, the distance between your longitudinal lines decreases (see a picture of a sphere with lat/lon lines if that doesn’t make sense.

Anyway, what that means is that the distance in the lon is relative to the lat, so that’s why the equation is a bit more than you’d expect. Altitude of sea level is assumed since that’s what defines the earth’s radius, so distance along the great arc from two points is defined as distance = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon2 - lon1)) * R (radius of earth). Don’t forget to keep units proper. Okay, so you can see the first part of the equation, sin(lat1)*sin(lat2) is the distance along the latitudinal part (north to south or south to north), but the distance east to west is dependent on the latitude. I think there’s an easier equation than this out there, but this should work for a simple spherical earth representation if that’s all you’re looking for.

Latitude and longitude are angular coordinates describing positions on the surface of a sphere. Longitude values range from -180 to 180 going around the sphere (with 0 being the prime meridian); Latitude values go up/down, ranging from -90 (South Pole) to +90 (North Pole), with 0 being the Equator.

With that in mind, if you consider a sphere of radius 1, centred at the point (0,0,0) such that the point where the prime meridian crosses the equator is at (0,0,1), it is easy to transform any point from lat/long to XYZ coordinates in 3D space as follows:

public float latitude;
public float longitude;

transform.position = Quaternion.AngleAxis(longitude, -Vector3.up) * Quaternion.AngleAxis(latitude, -Vector3.right) * new Vector3(0,0,1);