Floating Origin
The floating origin system allows you to create large scenes in your game without experiencing camera and UI shaking 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 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.
The two main components involved in the process are:
  • Floating Origin Manager: manages the entire process.
  • Floating Origin Object: Added to every object that needs to be repositioned in the scene when the floating origin manager updates. Camera, vehicles, projectiles, missiles, environment objects, etc.

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 several settings for you to set up in the inspector:
The 'Focused Transform' is the transform that is repositioned 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 several thousand units is acceptable for the 'Max Distance From Center' on the Floating Origin Manager.

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 Transform, and add a Floating Origin Object component only to the root transform.
Anything that the player will see that is instantiated in the scene will need a Floating Origin Object component.

Managing Position Changes

When Floating Origin Objects are moved, sometimes it can result in glitches with visual effects or code that involves world-space position information.
The Floating Origin Object component already automatically manages Trail Renderers and World Space Particles anywhere in its hierarchy.
If you need to manage anything during a transition, the Floating Origin Object component contains two functions you can override, as well as events you can hook into that correspond to them:
  • OnPreOriginShift function (along with the onPreOriginShift event) - called immediately before a floating origin position update.
  • OnPostOriginShift 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 OnPreOriginShift function and restore or modify it in the OnPostOriginShift function.

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 otherwise 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;
Last modified 24d ago