Logo

dev-resources.site

for different kinds of informations.

AR Game ~ Location Information ~

Published at
6/9/2024
Categories
unity3d
gamedev
location
api
Author
takeda1411123
Categories
4 categories in total
unity3d
open
gamedev
open
location
open
api
open
Author
13 person written this
takeda1411123
open
AR Game ~ Location Information ~

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.

Image description

Request from Unity

Also, you can request from Unity project. If you request, you can see the map tile like below image.

Image description

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.

Image description

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();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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.

location Article's
30 articles in total
Favicon
Handling Location Permissions in React Native
Favicon
Realtime Location Tracker & Communications
Favicon
Introducing GroupTrack: Simplified Location-Based Services for Flutter
Favicon
React Native Background Location Tracking
Favicon
Answer: Flutter geolocator package not retrieving location
Favicon
Adding location tracking to mobile apps (for Android)
Favicon
Flutter Backgroud Locator
Favicon
AR Game ~ Geospatial API ~
Favicon
AR Game ~ Location Information ~
Favicon
Unlocking the Power of Geolocation with IPStack's API
Favicon
How to implement UWB precise location system with TDOA technology(1)
Favicon
mylivelocation
Favicon
Expo GPS Location + Task Manager
Favicon
A Realtime location sharing App built with React
Favicon
Developing a Bluetooth Low energy-based application
Favicon
How to get address location from latitude and longitude in Angular15+
Favicon
Laravel 9 Get Current User Location Using IP Address
Favicon
Location without Google Services
Favicon
How to find the folder location of a command in linux?
Favicon
Search the hospitals using Huawei Map Kit, Site Kit and Location Kit in Patient Tracking Android app (Kotlin) – Part 5
Favicon
Add maps and location services to your apps
Favicon
Know your Lagos market. How to create a web map showing images with QGIS and host it for free.
Favicon
How to Automatically Fill Addresses in Lifestyle Apps
Favicon
How to fetch current location of user in Flutter?
Favicon
How To Get Current User Location In Laravel
Favicon
Create your own indoor maps using Azure Maps Creator
Favicon
Anyone Used Locator to find anyone true Location..?
Favicon
How to Get IP Address Information Using Python [2021]
Favicon
Find the Coordinates for a Location
Favicon
Geohash Open-Source library in TSQL for SQL Server

Featured ones: