Advertisement
Guest User

Floating Origin Script

a guest
Apr 22nd, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class FloatingOrigin : MonoBehaviour {
  7.  
  8.     [Header("Settings")]
  9.     [Tooltip("When Target's position reaches this magnitude/distance, Target and Scene Root transforms will be moved.")]
  10.     public float _threshold = 1000f;
  11.  
  12.     [Header("References")]
  13.     [Tooltip("This Transform we will use to calculate the distance from origin. It will be moved back to near origin.")]
  14.     public Transform _target;
  15.  
  16.     void Awake() {
  17.         if (_target == null)
  18.             _target = Camera.main.transform;
  19.     }
  20.  
  21.     void LateUpdate() {
  22.         Vector3 targetPos = _target.position;
  23.         targetPos.y = 0f;
  24.  
  25.         if (targetPos.magnitude > _threshold) {
  26.             foreach (GameObject rootGO in SceneManager.GetActiveScene().GetRootGameObjects()) {
  27.                 rootGO.transform.position -= targetPos;
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement