{"slug": "floating-origin-to-handle-large-worlds-in-unity", "title": "Floating origin to handle large worlds in Unity.", "summary": "The article describes a Unity script called `FloatingOrigin.cs` that prevents precision errors in large game worlds by shifting the scene's origin when a reference object moves beyond a set distance threshold. The script can update all open scenes or just the active one, and it also adjusts the positions of particle systems, trail renderers, and line renderers to maintain visual consistency during the shift.", "body_md": "FloatingOrigin.cs\n\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n// Based on the Unity Wiki FloatingOrigin script by Peter Stirling\n\n// URL: http://wiki.unity3d.com/index.php/Floating_Origin\n\nusing UnityEngine;\n\nusing UnityEngine.SceneManagement;\n\npublic class FloatingOrigin : MonoBehaviour\n\n{\n\n    [Tooltip(\"Point of reference from which to check the distance to origin.\")]\n\n    public Transform ReferenceObject = null;\n\n    [Tooltip(\"Distance from the origin the reference object must be in order to trigger an origin shift.\")]\n\n    public float Threshold = 5000f;\n\n    [Header(\"Options\")]\n\n    [Tooltip(\"When true, origin shifts are considered only from the horizontal distance to orign.\")]\n\n    public bool Use2DDistance = false;\n\n    [Tooltip(\"When true, updates ALL open scenes. When false, updates only the active scene.\")]\n\n    public bool UpdateAllScenes = true;\n\n    [Tooltip(\"Should ParticleSystems be moved with an origin shift.\")]\n\n    public bool UpdateParticles = true;\n\n    [Tooltip(\"Should TrailRenderers be moved with an origin shift.\")]\n\n    public bool UpdateTrailRenderers = true;\n\n    [Tooltip(\"Should LineRenderers be moved with an origin shift.\")]\n\n    public bool UpdateLineRenderers = true;\n\n    private ParticleSystem.Particle[] parts = null;\n\n    void LateUpdate()\n\n    {\n\n        if (ReferenceObject == null)\n\n            return;\n\n        Vector3 referencePosition = ReferenceObject.position;\n\n        if (Use2DDistance)\n\n            referencePosition.y = 0f;\n\n        if (referencePosition.magnitude > Threshold)\n\n        {\n\n            MoveRootTransforms(referencePosition);\n\n            if (UpdateParticles)\n\n                MoveParticles(referencePosition);\n\n            if (UpdateTrailRenderers)\n\n                MoveTrailRenderers(referencePosition);\n\n            if (UpdateLineRenderers)\n\n                MoveLineRenderers(referencePosition);\n\n        }\n\n    }\n\n    private void MoveRootTransforms(Vector3 offset)\n\n    {\n\n        if (UpdateAllScenes)\n\n        {\n\n            for (int z = 0; z < SceneManager.sceneCount; z++)\n\n            {\n\n                foreach (GameObject g in SceneManager.GetSceneAt(z).GetRootGameObjects())\n\n                    g.transform.position -= offset;\n\n            }\n\n        }\n\n        else\n\n        {\n\n            foreach (GameObject g in SceneManager.GetActiveScene().GetRootGameObjects())\n\n                g.transform.position -= offset;\n\n        }\n\n    }\n\n    private void MoveTrailRenderers(Vector3 offset)\n\n    {\n\n        var trails = FindObjectsOfType<TrailRenderer>() as TrailRenderer[];\n\n        foreach (var trail in trails)\n\n        {\n\n            Vector3[] positions = new Vector3[trail.positionCount];\n\n            int positionCount = trail.GetPositions(positions);\n\n            for (int i = 0; i < positionCount; ++i)\n\n                positions[i] -= offset;\n\n            trail.SetPositions(positions);\n\n        }\n\n    }\n\n    private void MoveLineRenderers(Vector3 offset)\n\n    {\n\n        var lines = FindObjectsOfType<LineRenderer>() as LineRenderer[];\n\n        foreach (var line in lines)\n\n        {\n\n            Vector3[] positions = new Vector3[line.positionCount];\n\n            int positionCount = line.GetPositions(positions);\n\n            for (int i = 0; i < positionCount; ++i)\n\n                positions[i] -= offset;\n\n            line.SetPositions(positions);\n\n        }\n\n    }\n\n    private void MoveParticles(Vector3 offset)\n\n    {\n\n        var particles = FindObjectsOfType<ParticleSystem>() as ParticleSystem[];\n\n        foreach (ParticleSystem system in particles)\n\n        {\n\n            if (system.main.simulationSpace != ParticleSystemSimulationSpace.World)\n\n                continue;\n\n            int particlesNeeded = system.main.maxParticles;\n\n            if (particlesNeeded <= 0)\n\n                continue;\n\n            // ensure a sufficiently large array in which to store the particles\n\n            if (parts == null || parts.Length < particlesNeeded)\n\n            {\n\n                parts = new ParticleSystem.Particle[particlesNeeded];\n\n            }\n\n            // now get the particles\n\n            int num = system.GetParticles(parts);\n\n            for (int i = 0; i < num; i++)\n\n            {\n\n                parts[i].position -= offset;\n\n            }\n\n            system.SetParticles(parts, num);\n\n        }\n\n    }\n\n}", "url": "https://wpnews.pro/news/floating-origin-to-handle-large-worlds-in-unity", "canonical_source": "https://gist.github.com/brihernandez/9ebbaf35070181fa1ee56f9e702cc7a5", "published_at": "2020-04-06 14:17:43+00:00", "updated_at": "2026-05-23 12:05:39.148628+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Unity"], "alternates": {"html": "https://wpnews.pro/news/floating-origin-to-handle-large-worlds-in-unity", "markdown": "https://wpnews.pro/news/floating-origin-to-handle-large-worlds-in-unity.md", "text": "https://wpnews.pro/news/floating-origin-to-handle-large-worlds-in-unity.txt", "jsonld": "https://wpnews.pro/news/floating-origin-to-handle-large-worlds-in-unity.jsonld"}}