Skip to content

Instantly share code, notes, and snippets.

@st4rdog
Created July 4, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st4rdog/5ae0d77fc972ecc97a16e0966f8212f1 to your computer and use it in GitHub Desktop.
Save st4rdog/5ae0d77fc972ecc97a16e0966f8212f1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
//http://wiki.unity3d.com/index.php?title=Floating_Origin
public class FloatingPointOriginMoveS : MonoBehaviour {
[Header("Settings")]
[Tooltip("When Target's position reaches this magnitude/distance, Target and Scene Root transforms will be moved.")]
public float _threshold = 1000f;
[Header("References")]
[Tooltip("This Transform we will use to calculate the distance from origin. It will be moved back to near origin.")]
public Transform _target;
void Awake()
{
if (_target == null)
_target = Camera.main.transform;
}
void LateUpdate()
{
Vector3 targetPos = _target.position;
targetPos.y = 0f;
if (targetPos.magnitude > _threshold)
{
foreach (GameObject rootGO in SceneManager.GetActiveScene().GetRootGameObjects())
{
rootGO.transform.position -= targetPos;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment