Created
July 4, 2019 13:24
-
-
Save st4rdog/5ae0d77fc972ecc97a16e0966f8212f1 to your computer and use it in GitHub Desktop.
This file contains 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.
Learn more about bidirectional Unicode characters
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