We need the following values in order to do proper geo-referencing operations in Unity:
(The following 2 values are found in TerraLand Downloader’s “Area Location” section)
- public double originLatitude; Origin Latitude coordinate of the database & generated terrain area
- public double originLongitude; Origin Longitude coordinate of the database & generated terrain area
36.87916, -111.5105
(The following 2 values are found in TerraLand Downloader’s “Area Size” section)
- public double areaHeight; Length of the generated area in meters
- public double areaWidth; Width of the generated area in meters
12000, 12000
(The following 4 values are found in TerraLand Downloader’s “Terrain Bounds” tab)
- public double areaTop; Top Latitude coordinate of the generated area
- public double areaBottom; Bottom Latitude coordinate of the generated area
- public double areaLeft; Left Longitude coordinate of the generated area
- public double areaRight; Right Longitude coordinate of the generated area
Top: 36.9330589170472, Bottom: 36.8252610829528, Left: -111.577881816826 & Right: -111.443118183174
And then we can simply convert any Latitude/Longitude points of interests into Unity’s world space in our scene. Here is the following script to get geo-referenced object position in Unity world space and move it to desired Latitude/Longitude coordinates right in the Editor.
-
usingย System.Collections;
-
usingย System.Collections.Generic;
-
usingย UnityEngine;
-
[ExecuteInEditMode]
-
publicย classย LatLon2Unityย :ย MonoBehaviour
-
{
-
ย ย ย publicย doubleย originLatitude;
-
ย ย ย publicย doubleย originLongitude;
-
ย ย ย publicย doubleย areaHeight;
-
ย ย ย publicย doubleย areaWidth;
-
ย ย ย publicย doubleย areaTop;
-
ย ย ย publicย doubleย areaBottom;
-
ย ย ย publicย doubleย areaLeft;
-
ย ย ย publicย doubleย areaRight;
-
ย ย ย publicย boolย forceMoveToLatLonย =ย false;
-
ย ย ย publicย doubleย destinationLat;
-
ย ย ย publicย doubleย destinationLon;
-
ย ย ย privateย doubleย currentLatitude;
-
ย ย ย privateย doubleย currentLongitude;
-
ย ย ย voidย Updateย ()
-
ย ย ย {
-
ย ย ย ย ย if(transform.position.zย >=ย 0)
-
ย ย ย ย ย ย currentLatitudeย =ย originLatitudeย +ย ((areaTopย –ย areaBottom)ย *ย (Mathf.InverseLerp(0,ย (float)areaHeight, transform.position.z)));
-
ย ย ย ย ย else
-
ย ย ย ย ย ย currentLatitudeย =ย originLatitudeย –ย ((areaTopย –ย areaBottom)ย *ย (Mathf.InverseLerp(0,ย (float)areaHeight,ย -transform.position.z)));
-
ย ย ย ย ย if(transform.position.xย >=ย 0)
-
ย ย ย ย ย ย currentLongitudeย =ย originLongitudeย +ย ((areaRightย –ย areaLeft)ย *ย (Mathf.InverseLerp(0,ย (float)areaWidth, transform.position.x)));
-
ย ย ย ย ย else
-
ย ย ย ย ย ย currentLongitudeย =ย originLongitudeย –ย ((areaRightย –ย areaLeft)ย *ย (Mathf.InverseLerp(0,ย (float)areaWidth,ย -transform.position.x)));
-
ย ย ย ย ย print(“Current Lat: ”ย +ย currentLatitudeย +ย ” ย Lon: ”ย +ย currentLongitude);
-
ย ย ย ย ย if(forceMoveToLatLon)
-
ย ย ย ย ย ย MoveToLatLon();
-
ย ย ย }
-
ย ย ย privateย voidย MoveToLatLonย ()
-
ย ย ย {
-
ย ย ย ย ย ย ย (
-
ย ย ย ย ย ย ย ย ย (float)(((destinationLonย –ย areaLeft)ย /ย (areaRightย –ย areaLeft))ย *ย (areaWidth))ย –ย (float)(areaWidthย /ย 2f),
-
ย ย ย ย ย ย ย ย ย 0,
-
ย ย ย ย ย ย ย ย ย (float)(((destinationLatย –ย areaBottom)ย /ย (areaTopย –ย areaBottom))ย *ย (areaHeight))ย –ย (float)(areaHeightย /ย 2f)
-
ย ย ย ย ย ย ย );
-
ย ย ย }
-
}
Following picture shows a geo-referenced object placed exactly at given coordinates of “36.9005219376448, -111.458823759922”.ย Location on Google Maps
Here is the free download of full scene for the above implementation:
terraunity.com/freedownload/TerraLand_LatLon2UnityWorldSpace.unitypackage
FYI, the “TerraLand Terrain” component has built-in coordinates converter between “Minute, Degree, Second” (MDS) to Decimal Degrees (DD) GIS formats and vice versa, so no need to go for online converters such as the one you mentioned.