Floating Origin


Unity stores the position values of GameObjects in single-precision floating-point format. This works well for small values, which it can represent precisely; but precision decreases as values increase because of floating-point rounding errors. As a result, GameObjects rendered far from the origin aren't positioned accurately (they're slightly misplaced). The effect becomes more pronounced the further the gameplay moves from the origin, and it's most noticeable during animations. So for example, instead of the player moving smoothly through the scene, their movement appears jittery.


The Maps Unity SDK addresses this problem with a method called MoveFloatingOrigin. This method re-centers the gameworld's coordinates upon a new position (the current camera position for example)—seamlessly repositioning the world, the player, and the camera. The only changes are the position values of everything in the world.


The Maps Unity SDK includes an example scene that demonstrates how to re-center the gameworld using the MoveFloatingOrigin method. You can find this scene in Unity's Project pane, inside the Assets folder, under GoogleMaps > Examples. The scene contains a GameObject called Floating Origin Example Script, which contains a script component called Floating Origin Updater (Script), which references the C# source file named FloatingOriginUpdater.cs.

The script waits for the camera to move a specific distance from the origin, and then it re-centers the gameworld. The following code snippet provides an example of how to call the MoveFloatingOrigin method.

    new [] { Camera.main.gameObject });

The floatingOrigin parameter

The first parameter is called floatingOrigin. It's a Vector3 that represents the position to re-center the gameworld around. If for example, you passed the position value (100, 0, 100), then that point would become the new origin (0, 0, 0), and the Maps Unity SDK would translate all of the map feature GameObjects by that offset.

In the code snippet above, we passed the current position of the camera for the value of floatingOrigin. This moves the origin to the current camera position, effectively re-centering the gameworld around the current view.

The gameObjects parameter

The second parameter is called gameObjects, and its use is optional. It's an array of GameObjects to move at the same time that the floating origin is moved. The Maps Unity SDK automatically translates all of the GameObjects that it creates (geographic features such as buildings and roads), but this parameter allows you to specify extra GameObjects—such as the player, and non-playable characters such as monsters and treasures.

In the code snippet above, we passed a new array that contains just the camera for the value of gameObjects. This allows us to translate the camera to the new origin.

Send feedback about...

Google Maps Platform gaming solution
Google Maps Platform gaming solution