Floating Origin

The floating origin system allows you to create large scenes in your game without experiencing the shaking of the camera and UI that typically occcurs when the player gets too far from the scene center.
The problem exists because, at larger distances, more bits in the floating point number are used up by the whole number part of the position values, and fewer bits are available for decimal points.

How It Works

The way that the floating origin system deals with this is by periodically moving the player back toward the center of the scene, and adjusting the relative position of everything else to make it look as if nothing changed.

Floating Origin Manager

To set up a scene to use the floating origin, first add a Floating Origin Manager component anywhere in your scene. This is a singleton, so there must be only one of them in the scene at any time.
There are a couple of important settings in the inspector
The 'Focused Transform' is the transform that is re-positioned back at the center of the scene when the floating origin is updated. Usually this is either the player vehicle or the vehicle camera.
The 'Max Distance From Center' is how far the 'Focused Transform' can move away from the center before it is moved back. Generally, anywhere up to a couple of thousand units gives a good result.

Floating Origin Objects

A floating origin object is any object that must be re-positioned relative to the player when the floating origin is updated.
Usually this includes any part of the visible scenery that moves relative to the player, including asteroids, space stations, ships, projectiles, missiles, environment assets.
It may simplify things to create the environment under one root Transform, and add a Floating Origin Object component only to the root Transform.

Managing Position Changes

When a Floating Origin Object is moved, it can sometimes result in glitches with visual effects, or problems with code that involves world-space position information. To avoid this, the Floating Origin Object component contains two functions you can override, as well as events you can hook into that correspond to them:
  • 'On Pre Origin Shift' function (along with the 'onPreOriginShift' event) - called immediately before a floating origin position update.
  • 'On Post Origin Shift' function (along with the 'onPostOriginShift' event) - called immediately after a floating origin position update.
These are useful because you can store state information in the 'On Pre Origin Shift' function and restore or modify it in the 'On Post Origin Shift' function.
The Floating Origin Object component already automatically manages Trail Renderers and World Space Particles anywhere in its hierarchy.

Getting The Floating Origin Position

If the player keeps getting moved back to the center as they move away from the origin, it may be difficult to know what the actual position of an object in the scene is with reference to the original scene center.
You can easily access this information with the following code:
// Get the 'real' position of some transform
Vector3 realPosition = myTransform.position - FloatingOriginManager.Instance.FloatingOriginPosition;

Activating/Deactivating The Floating Origin

Sometimes you may want to temporarily deactivate the floating origin manager, for example when playing a cutscene that would be interfered with by a floating origin shift.
To do this, you can activate or deactivate the floating origin with the following code:
// Activate the floating origin
FloatingOriginManager.Instance.Activated = true;
// Deactivate the floating origin
FloatingOriginManager.Instance.Activated = false;