In the last Unity3D article, some of the features in the program were mentioned. In this article, we would like to mention one interesting feature, which is the use of GPS in Unity3D, especially for beginners. Because the program has relatively simple use of GPS.

The programming language used in programming is C#, which is an object-oriented language. Therefore, it must be written as a class. The program has already prepared the necessary classes to use which is MonoBehaviour, which we can inherit and use immediately as GPS.cs file.

GPS.cs

This is to check the status of GPS. If the user has no GPS on, the program will alert the user to turn on the GPS and wait 20 seconds if there is no GPS signal, the program will notify and stop searching.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GPS : MonoBehaviour {
    public float latitude, longitude;
    public Text status;
    public static GPS Instance{set; get;}
    public bool succ = false;
    public bool scene2 = false;

	void Start () {
        Instance = this;
        DontDestroyOnLoad(gameObject);
        StartCoroutine(StartLocationService());
	}
	
    private IEnumerator StartLocationService()
    {
        
            if (!Input.location.isEnabledByUser)
            {
                status.text = "Please enable GPS";
                yield break;
            }
            Input.location.Start();
            int maxWait = 20;
            while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
            {
                yield return new WaitForSeconds(1);
                maxWait--;
            }
            if (maxWait <= 0)
            {
                status.text = "Time out";
                Debug.Log("Time out");
                yield break;
            }
            if (Input.location.status == LocationServiceStatus.Failed)
            {
                status.text = "can't determine device location";
                yield break;
            }        
        succ = true;
        scene2 = true;
        yield break;
    }
	void Update () {
        if (succ)
        {
            latitude = Input.location.lastData.latitude;
            longitude = Input.location.lastData.longitude;
        }
    }
}

After successfully checking the GPS signal, we can retrieve the latitude and longitude positions. In the example, GPSCheck.cs checks whether the user’s location is in a given location.

GPSCheck.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using UnityEngine.SceneManagement;
public class GPSCheck : MonoBehaviour {

	private double[] t1Lat1 = { 12.073144, 12.073072, 12.073142, 12.073066 },
        t1Long1 = { 98.977573, 98.977581, 98.977794, 98.97794 },
        t1Lat2 = { 12.073142, 12.073066, 12.073152, 12.073070 },
        t1Long2 = { 98.977794, 98.977794, 98.978003, 98.978015 },
        t1Lat3 = { 12.073152, 12.073070, 12.073150, 12.073056 },
        t1Long3 = { 98.978003, 98.978015, 98.978198, 98.978208 };
		
    public float lat, lon;
    private float minLat = 9999999.00f, maxLat = -1.0f, minLong = 9999999.00f, maxLong = -1.0f;
	
    void Start () {
        minLat = (float)pbruLat.Min();
        maxLat = (float)pbruLat.Max();
        minLong = (float)pbruLong.Min();
        maxLong = (float)pbruLong.Max();
    }
	
	void Update () {
		lat = GPS.Instance.latitude;
        lon = GPS.Instance.longitude;
		
		if ((lat <= t1Lat1.Max() && lat >= t1Lat1.Min() && lon <= t1Long1.Max() && lon >= t1Long1.Min()) ||
                (lat <= t1Lat2.Max() && lat >= t1Lat2.Min() && lon <= t1Long2.Max() && lon >= t1Long2.Min()) ||
                (lat <= t1Lat3.Max() && lat >= t1Lat3.Min() && lon <= t1Long3.Max() && lon >= t1Long3.Min()))
            {
				//do something
			}
	}
}

Verifying a location requires both latitude and longitude, which requires multiple comparisons. It checks if the variables lat and lon are between the minimum and maximum values of each set. This type of inspection can only be performed on rectangular areas.

Conclusion

This article is just a few examples of how GPS can be implemented in Unity3D by inheriting properties from MonoBehaviour that are already available. Before using the GPS, it is necessary to check whether the signal is available or not to reduce errors. We hope it will be more or less useful to all of you and have fun programming.

(C) 2022,By Jarut Busarathid and Danai Jedsadathitikul
Updated 2022-02-20