# 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.&#x20;

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.&#x20;

### Scene Setup

To set up a scene to use the floating origin:

1. 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.
2. Customize the _Max Distance From Center_ value (how far the camera gets from scene center before repositioning)

### 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.&#x20;

Usually this includes any part of the visible scenery that changes position relative to the player, including asteroids, space stations, ships, projectiles, missiles, environment assets, and more

#### Object Setup

To create a floating origin object, simply add a **Floating Origin Object** to it. It will automatically be repositioned when the origin moves.

{% hint style="info" %}
It may simplify things to create static stuff such as environment objects under one root Transform, and add a **Floating Origin Object** component to it.
{% endhint %}

#### Events/Functions

When a **Floating Origin Object** is moved, it can sometimes result in glitches involving things that rely on world-space position information.&#x20;

To avoid this, the **Floating Origin Object** component contains two functions you can override to manage problems:

* _On Pre Origin Shift_ - called immediately before a floating origin position update.
* _On Post Origin Shift_ - called immediately after a floating origin position update.

You can store state information in the _On Pre Origin Shift_ function and restore or modify it in the _On Post Origin Shift_ function.

{% hint style="info" %}
The **Floating Origin Object** component already automatically manages **Trail Renderers** and **World Space Particles** placed anywhere in its hierarchy.
{% endhint %}

### 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.

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;
```
