Search Unity

Floating-Origin Script (Any recommendations?)

Discussion in 'Scripting' started by AtlasT2022, Jul 2, 2022.

  1. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    I just finished a floating-origin script. Any recommendations?
    Code (CSharp):
    1.  
    2. /*
    3. To use this script:
    4.  
    5.  
    6. 1.  Create an object named "World" or anything you want.
    7. 2.  Drag all objects but the player or main object, whatever your case is.
    8. 3.  Attach the script to the "World" object.
    9. 4.  Assign the "Player" or main object into the "Player" variable in the inspector.
    10. 5. Start it up.
    11. */
    12. using System.Collections;
    13. using System.Collections.Generic;
    14. using UnityEngine;
    15. public class FloatingOrigin : MonoBehaviour
    16. {
    17.     public GameObject Player;
    18.     public bool hasReached10mil;
    19.     public bool hasReached100mil;
    20.     public bool hasReached1bil;
    21.  
    22.     // Update is called once per frame
    23.     void FixedUpdate()
    24.     {
    25.         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)
    26.         {
    27.         hasReached10mil = true;
    28.         }
    29.         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)
    30.         {
    31.             hasReached100mil = true;
    32.         }
    33.         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)
    34.         {
    35.             hasReached1bil = true;
    36.         }
    37.             if (hasReached1bil != true)
    38.             {
    39.                 if(hasReached100mil != true)
    40.                 {
    41.                 if (hasReached10mil != true)
    42.                 {
    43.                     if (Player.transform.position.x < -10)
    44.                     {
    45.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    46.                         transform.position += (new Vector3(10, 0, 0));
    47.                     }
    48.                     if (Player.transform.position.y < -10)
    49.                     {
    50.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    51.                         transform.position += (new Vector3(0, 10, 0));
    52.                     }
    53.                     if (Player.transform.position.z < -10)
    54.                     {
    55.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    56.                         transform.position += (new Vector3(0, 0, 10));
    57.                     }
    58.                     if (Player.transform.position.x > 10)
    59.                     {
    60.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    61.                         transform.position += (new Vector3(-10, 0, 0));
    62.                     }
    63.                     if (Player.transform.position.y > 10)
    64.                     {
    65.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    66.                         transform.position += (new Vector3(0, -10, 0));
    67.                     }
    68.                     if (Player.transform.position.z > 10)
    69.                     {
    70.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    71.                         transform.position += (new Vector3(0, 0, -10));
    72.                     }
    73.                 }
    74.                     if (hasReached10mil == true)
    75.                     {
    76.                     if (Player.transform.position.x < -100)
    77.                     {
    78.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    79.                         transform.position += (new Vector3(100, 0, 0));
    80.                     }
    81.                     if (Player.transform.position.y < -100)
    82.                     {
    83.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    84.                         transform.position += (new Vector3(0, 100, 0));
    85.                     }
    86.                     if (Player.transform.position.z < -100)
    87.                     {
    88.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    89.                         transform.position += (new Vector3(0, 0, 100));
    90.                     }
    91.                     if (Player.transform.position.x > 100)
    92.                     {
    93.                         Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    94.                         transform.position += (new Vector3(-100, 0, 0));
    95.                     }
    96.                     if (Player.transform.position.y > 100)
    97.                     {
    98.                         Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    99.                         transform.position += (new Vector3(0, -100, 0));
    100.                     }
    101.                     if (Player.transform.position.z > 100)
    102.                     {
    103.                         Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    104.                         transform.position += (new Vector3(0, 0, -100));
    105.                     }
    106.                 }
    107.                 }
    108.                 if (hasReached100mil == true)
    109.                 {
    110.                 if (Player.transform.position.x < -1000)
    111.                 {
    112.                     Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    113.                     transform.position += (new Vector3(1000, 0, 0));
    114.                 }
    115.                 if (Player.transform.position.y < -1000)
    116.                 {
    117.                     Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    118.                     transform.position += (new Vector3(0, 1000, 0));
    119.                 }
    120.                 if (Player.transform.position.z < -1000)
    121.                 {
    122.                     Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    123.                     transform.position += (new Vector3(0, 0, 1000));
    124.                 }
    125.                 if (Player.transform.position.x > 1000)
    126.                 {
    127.                     Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    128.                     transform.position += (new Vector3(-1000, 0, 0));
    129.                 }
    130.                 if (Player.transform.position.y > 1000)
    131.                 {
    132.                     Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    133.                     transform.position += (new Vector3(0, -1000, 0));
    134.                 }
    135.                 if (Player.transform.position.z > 1000)
    136.                 {
    137.                     Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    138.                     transform.position += (new Vector3(0, 0, -1000));
    139.                 }
    140.             }
    141.             }
    142.             if (hasReached1bil == true)
    143.             {
    144.             if (Player.transform.position.x < -10000)
    145.             {
    146.                 Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    147.                 transform.position += (new Vector3(10000, 0, 0));
    148.             }
    149.             if (Player.transform.position.y < -10000)
    150.             {
    151.                 Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    152.                 transform.position += (new Vector3(0, 10000, 0));
    153.             }
    154.             if (Player.transform.position.z < -10000)
    155.             {
    156.                 Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    157.                 transform.position += (new Vector3(0, 0, 10000));
    158.             }
    159.             if (Player.transform.position.x > 1000)
    160.             {
    161.                 Player.transform.position = (new Vector3(0, Player.transform.position.y, Player.transform.position.z));
    162.                 transform.position += (new Vector3(-1000, 0, 0));
    163.             }
    164.             if (Player.transform.position.y > 1000)
    165.             {
    166.                 Player.transform.position = (new Vector3(Player.transform.position.x, 0, Player.transform.position.z));
    167.                 transform.position += (new Vector3(0, -1000, 0));
    168.             }
    169.             if (Player.transform.position.z > 1000)
    170.             {
    171.                 Player.transform.position = (new Vector3(Player.transform.position.x, Player.transform.position.y, 0));
    172.                 transform.position += (new Vector3(0, 0, -1000));
    173.             }
    174.         }
    175.     }
    176. }
     
  2. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    Allows you to go out to even 100,000,000,000 unity units before stuff becomes jittery.


    Edit: I should also note that to teleport, move the world object to the opposite of the position you want the player to go, and teleport the player to
    Code (CSharp):
    1. Vector3.zero
    , just to make it compatible with the floating-origin.
     
    Last edited: Jul 2, 2022
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,190
    Single precision floating points can only handle seven digits of accuracy. That's more than seven.
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Well, you can certainly make it more readable by moving the common code to a function.

    Code (csharp):
    1.  
    2.      void adjustPosition(Vector3 playerPos, Vector3 transformOffset)
    3.       {
    4.            Player.transform.position = playerPos;
    5.            transform.position += transformOffset;
    6.       }
    7.  
    You can also put the limits in arrays or lists and then do while loops, but that requires more thinking.
     
    Last edited: Jul 2, 2022
    jvo3dc, AtlasT2022 and Kurt-Dekker like this.
  5. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    Works just fine.
     
  6. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    I increase the value that it moves the world to after the player reaches a certain point.
     
  7. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    True, but before precision loss. Yes, I know this is very late.