- While my posts are mainly focused on Unity development, this one is applicable to any mobile application.
For many games, specially mobile games, knowing the user’s location can be helpful. For example you could have leader-boards based on your users’ countries and have them compete in masses. For games that have a social aspect to them, location can be good proxy to suggest communities and match up users if there is coop or PvP modes involved. Some games like Pokemon GO and Ingress are entirely location-based and you can’t play them without sharing your location. These games are on the rise, even though it is uncertain what percentage of gamers will play these genre of games as they require moving around in physical world which is not convenient.
At any rate, location is something that comes up during development and if it has come up for you, it’s time to dive in and see how to get user’s location in Unity.
There are three ways to acquire a user’s location:
1. Asking the user for their location
It is obvious and the most simple to implement from a technical standpoint. You’d need some basic UI that the user would fill out. It could be a dropdown of possible options or a text field for more general scenarios. You could get more fancy and show the user a map.
You can’t really trust the user to provide you with their true location. Will they get a reward for moving around or being in a certain country or region? You bet they will choose that location.
2. IP-based Geolocation
Using user’s IP address is another popular option. If you don’t know what an IP (Internet Protocol) address is, it is a unique address that is assigned to each device on the internet. Very similar to physical addresses but more dynamic and only somewhat related to physical location. There are many online websites that let you perform IP location lookups.
How this works essentially is that the main nodes on the internet have static IPs (ISPs, cell towers, data centers, etc.) and these services keep a mapping of those IPs to locations.
This method is convenient and non-intrusive. You don’t need any action from the user to get their location. They however need to have an active internet connection. If the user is offline, well then you won’t have their location.
The accuracy of the location is the lowest of the methods presented here. The location is not even the user’s location but their internet provider’s location… approximated! It may however be good enough if you don’t heavily rely on the accuracy of the data. For example to decide the user’s country to set their default language, this method could work.
Implementation in Unity is straightforward. You can easily find IP geolocation websites with web APIs. You could make a regular http call using UnityWebRequest class in Unity. In my projects I use GPS location, so I have never actually implemented this. If you would like to see a howto post leave me a comment. I might get around to do it at some point.
3. From their GPS sensor
GPS is the most accurate of the location methods. It is up to a few meters accurate. The downside is that you got to explicitly get user’s permission and be careful not to deplete the device’s battery by keeping the location service running unnecessarily. Also there are location spoofing apps on both Android and iOS, so you still can’t trust the location 100%, but for all practical purposes, this is the way to go.
Unity has a
LocationService class which handles accessing device’s GPS location. It has some nuances but makes the job easy.
If you want to go with this option, check out at my post on using the location sensor in Unity.
As a software engineer turned indie game developer, I write about the technical problems I encounter. Follow me here or on instagram (IG:@no_such_studio ) to learn more about game development, Unity and programming.