Table of contents
- Background
- Creation of AR game with location information
- Execution of the game
Background
I will develop AR Game with Unity, AR foundation and so on. To learn AR development, I am researching about AR and the software related it. This blog shows the research and the process of developing AR game. If you have a question, I am happy to answer it.
Creation of AR game with location information
In our game, users' location information will be utilized for showing user on real map. When players move in real move, their characters on the map move. This post will show how to develop the AR game with location information.
Technology
These below technologies will be used in this game.
- Google Maps API
- CUDLR
Google Maps API
At this time, Google Maps API will be utilized to show users' character on the map. In google map apps, the map is showed by using still image tile.
In addition to this map information, the Google Maps API can dynamically change the way it is displayed, so you can view the map from the view point of a TPS user. The map display can also be changed to make it look more like a game.
API Key
It is necessary to get API keys to use Google Maps API.
You can get it referencing the below URL.
https://developers.google.com/maps/documentation/maps-static/get-api-key
Sample Request
If you get API Key, you can get static image tile by requesting Map Static API like the below URL.
https://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&key={Your API Key}
- {Your API key} can be changed to your API key.
Request from Unity
Also, you can request from Unity project. If you request, you can see the map tile like below image.
CUDLR
CUDLR is one of the debug tool. Log can be seen on web while the game is running on your devices.
Important Points
CUDLR is for debug or test, so do not use for production.
Please make sure to delete CUDLR objects before release.
Get users' location
This game requires synchronisation of the user's actual position with the player position in the game. For this purpose, the actual position of the user needs to be acquired.
Permission for getting user location
This game requires user permission for location data to be acquired, which can be set on PlayerSettings.
Code sample
You can get user location information on Unity.
// check if the program has the user permission
if (!Input.location.isEnabledByUser)
{
Debug.Log("Location services are not available");
yield break;
}
// start location service
Input.location.Start();
// wait for the Initializing services
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// get location information
Debug.Log(
"Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude
);
// stop location service
Input.location.Stop();
Get users rotation
For a better user experience, the orientation of the player is also changed depending on the orientation of the device.
Code sample
// enable compass
Input.compass.enabled = true;
// change the rotation of the player depending on the rotation of the device
var heading = 180 + Input.compass.magneticHeading;
var rotation = Quaternion.AngleAxis(heading, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedTime * .001f);
Execution of the game
Merging these above implementation, players can see the map around the actual user position. Also, they can see around while changing the rotation of devices.
Next Step
As next step, I will research Geospatial API for the development using outdoor buildings.
Top comments (0)