Astronaut game
This is an enhanced version of the location-based game example. It features custom styling, procedural vehicle movement based on road data, and dynamic lighting. Click on the road in the editor to navigate the astronaut character to a point using our Directions API. When run on a device, this example uses GPS location information to move and rotate the character appropriately with the device.
Locate the player
The LocationProvider
GameObject holds a collection of scripts to determine a player’s location, either in the real world (with GPS, on a device) or simulated in Unity (with manually added coordinates). Without these scripts, the location would be derived from the AbstractMap
script.
There are several different Location Provider scripts:
DeviceLocationProvider
: Uses GPS data on a device. TheMapboxAstronaut
GameObject follows the location on the map reported by the GPS device update.EditorLocationProvider
: is likeDeviceLocationProvider
, but you can provide coordinates for debugging in the editor rather than GPS.LocationArrayEditorLocationProvider
: Allows the GameObject to be moved across a set of points entered as latitude, longitude coordinates in an array.TransformLocationProvider
: Makes the GameObject follow the position and rotation of another GameObject.DeviceLocationProviderAndroidNative
: Uses GPS data optimized for Android devices.
Directions
This example shows how Mapbox Navigation can be used inside of Unity with the DirectionsFactory.cs script.
The player in the scene is the MapboxAstronaut
GameObject. In the hierarchy, click on MapboxAstronaut and you'll see that it contains a click-to-move controller, a custom DirectionsFactory script called AstronautDirections. When a point on the map is clicked, the route between the astronaut and the point on the map will be returned via a query to the Directions API, and the astronaut will move to the point clicked following that route.
For more information on navigation inside of Unity, see the Traffic and Directions documentation.
Traffic
In this scene, traffic is visualized by leveraging the road data from the Mapbox Streets v7 tileset. The tileset is specified on the AbstractMap inside the Map Layers settings as a Custom
Data Source as shown in the screenshot below. You can specify multiple tilesets as data sources by separating them with a comma. For more information refer to the documentation on using custom data in Unity.
Map Features are used to render the road data and car animations. Navigate to Map Layers > Features > Map Features
, and click on the Traffic layer to see how the visualizers are configured. Open the Advanced > Filters
section to see how you can filter the Mapbox Streets v7 tileset data to display exactly what is needed for your use case. The road
layer of the tileset contains the class
key, which has the values major
, street
, tertiary
, and primary
specified. Set Combiner Type to Any to show all roads that have the specified Str Value. Filters are a robust way to show as much or as little data as you need for your scene.
To see how the streets are styled with moving cars, open the Behavior Modifiers section. A custom LoftModifier is specified for the Mesh Modifiers. This provides depth for the cars moving on the street. Additionally, the Game Object Modifiers section holds a custom version of a MaterialModifier, which has the materials used to style the top and sides of the roads.
The animation effect is handled by the TrafficAnimator
object, which also contains the same RoadMaterial
.
This example scene is a good starting point for a location-based game with directions and polished styling. See the documentation for a location-based game example.