Search Unity

Showcase Floating-Origin Script

Discussion in 'World Building' started by AtlasT2022, Jul 1, 2022.

  1. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    I just finished a floating-origin script that I want to share.
    Please point out any bugs or recommendations.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class FloatingOrigin : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.     public bool hasReached10mil;
    9.     public bool hasReached100mil;
    10.     public bool hasReached1bil;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (transform.position.x > 10000000 | transform.position.y > 10000000 | transform.position.z > 10000000 | transform.position.x < -10000000 | transform.position.y < -10000000 | transform.position.z < -10000000)
    20.         {
    21.         hasReached10mil = true;
    22.         }
    23.         if (transform.position.x > 100000000 | transform.position.y > 100000000 | transform.position.z > 100000000 | transform.position.x < -100000000 | transform.position.y < -100000000 | transform.position.z < -100000000)
    24.         {
    25.             hasReached100mil = true;
    26.         }
    27.         if (transform.position.x > 1000000000 | transform.position.y > 1000000000 | transform.position.z > 1000000000 | transform.position.x < -1000000000 | transform.position.y < -1000000000 | transform.position.z < -1000000000)
    28.         {
    29.             hasReached1bil = true;
    30.         }
    31.             if (hasReached1bil != true)
    32.             {
    33.                 if(hasReached100mil != true)
    34.                 {
    35.                 if (hasReached10mil != true)
    36.                 {
    37.                     if (Player.transform.position.x < -10)
    38.                     {
    39.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    40.                         transform.position += (new Vector3(10, 0, 0));
    41.                     }
    42.                     if (Player.transform.position.y < -10)
    43.                     {
    44.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    45.                         transform.position += (new Vector3(0, 10, 0));
    46.                     }
    47.                     if (Player.transform.position.z < -10)
    48.                     {
    49.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    50.                         transform.position += (new Vector3(0, 0, 10));
    51.                     }
    52.                     if (Player.transform.position.x > 10)
    53.                     {
    54.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    55.                         transform.position += (new Vector3(-10, 0, 0));
    56.                     }
    57.                     if (Player.transform.position.y > 10)
    58.                     {
    59.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    60.                         transform.position += (new Vector3(0, -10, 0));
    61.                     }
    62.                     if (Player.transform.position.z > 10)
    63.                     {
    64.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    65.                         transform.position += (new Vector3(0, 0, -10));
    66.                     }
    67.                 }
    68.                     if (hasReached10mil == true)
    69.                     {
    70.                     if (Player.transform.position.x < -100)
    71.                     {
    72.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    73.                         transform.position += (new Vector3(100, 0, 0));
    74.                     }
    75.                     if (Player.transform.position.y < -100)
    76.                     {
    77.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    78.                         transform.position += (new Vector3(0, 100, 0));
    79.                     }
    80.                     if (Player.transform.position.z < -100)
    81.                     {
    82.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    83.                         transform.position += (new Vector3(0, 0, 100));
    84.                     }
    85.                     if (Player.transform.position.x > 100)
    86.                     {
    87.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    88.                         transform.position += (new Vector3(-100, 0, 0));
    89.                     }
    90.                     if (Player.transform.position.y > 100)
    91.                     {
    92.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    93.                         transform.position += (new Vector3(0, -100, 0));
    94.                     }
    95.                     if (Player.transform.position.z > 100)
    96.                     {
    97.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    98.                         transform.position += (new Vector3(0, 0, -100));
    99.                     }
    100.                 }
    101.                 }
    102.                 if (hasReached100mil == true)
    103.                 {
    104.                 if (Player.transform.position.x < -1000)
    105.                 {
    106.                     Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    107.                     transform.position += (new Vector3(1000, 0, 0));
    108.                 }
    109.                 if (Player.transform.position.y < -1000)
    110.                 {
    111.                     Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    112.                     transform.position += (new Vector3(0, 1000, 0));
    113.                 }
    114.                 if (Player.transform.position.z < -1000)
    115.                 {
    116.                     Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    117.                     transform.position += (new Vector3(0, 0, 1000));
    118.                 }
    119.                 if (Player.transform.position.x > 1000)
    120.                 {
    121.                     Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    122.                     transform.position += (new Vector3(-1000, 0, 0));
    123.                 }
    124.                 if (Player.transform.position.y > 1000)
    125.                 {
    126.                     Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    127.                     transform.position += (new Vector3(0, -1000, 0));
    128.                 }
    129.                 if (Player.transform.position.z > 1000)
    130.                 {
    131.                     Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    132.                     transform.position += (new Vector3(0, 0, -1000));
    133.                 }
    134.             }
    135.             }
    136.             if (hasReached1bil == true)
    137.             {
    138.             if (Player.transform.position.x < -10000)
    139.             {
    140.                 Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    141.                 transform.position += (new Vector3(10000, 0, 0));
    142.             }
    143.             if (Player.transform.position.y < -10000)
    144.             {
    145.                 Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    146.                 transform.position += (new Vector3(0, 10000, 0));
    147.             }
    148.             if (Player.transform.position.z < -10000)
    149.             {
    150.                 Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    151.                 transform.position += (new Vector3(0, 0, 10000));
    152.             }
    153.             if (Player.transform.position.x > 1000)
    154.             {
    155.                 Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    156.                 transform.position += (new Vector3(-1000, 0, 0));
    157.             }
    158.             if (Player.transform.position.y > 1000)
    159.             {
    160.                 Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    161.                 transform.position += (new Vector3(0, -1000, 0));
    162.             }
    163.             if (Player.transform.position.z > 1000)
    164.             {
    165.                 Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    166.                 transform.position += (new Vector3(0, 0, -1000));
    167.             }
    168.         }
    169.     }
    170. }
    171.  
    172.  
    To use this, create a "World"
    GameObject and attach this script to it.
    Then assign the player or main GameObject
    to it and drag all but the player or main GameObjects into it.
    You can go out to 10 billion before any noticeable changes.

    I should note that if you are moving a GameObject with a script, it may move infinitely in some cases, so make sure to make it compatible with the floating origin.
     
    Last edited: Jul 2, 2022
    warthos3399 and Deleted User like this.