Search Unity

Feedback New better Floating-Origin (Than my previous)

Discussion in 'Scripting' started by AtlasT2022, Sep 22, 2022.

  1. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    I've finished a smaller Floating-Origin that will prevent bugs like an object being jittery from being so far away from the World object (What I previously used).
    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FloatingOrigin : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.     private GameObject[] FloatingObjects;
    9.     public string Tag = "Floating";
    10.  
    11.     // FixedUpdate is called twice per frame
    12.     void FixedUpdate()
    13.     {
    14.         // Get objects with tag
    15.         FloatingObjects = GameObject.FindGameObjectsWithTag(Tag);
    16.  
    17.         // X
    18.         if (Player.transform.position.x > 10)
    19.         {
    20.             Player.transform.position += new Vector3(-10, 0, 0);
    21.             foreach (GameObject selected in FloatingObjects)
    22.                 selected.transform.position += new Vector3(-10, 0, 0);
    23.         }
    24.         else if (Player.transform.position.x < -10)
    25.         {
    26.             Player.transform.position += new Vector3(10, 0, 0);
    27.             foreach (GameObject selected in FloatingObjects)
    28.                 selected.transform.position += new Vector3(10, 0, 0);
    29.         }
    30.  
    31.  
    32.         // Y
    33.         if (Player.transform.position.y > 10)
    34.         {
    35.             Player.transform.position += new Vector3(0, -10, 0);
    36.             foreach (GameObject selected in FloatingObjects)
    37.                 selected.transform.position += new Vector3(0, -10, 0);
    38.         }
    39.         else if (Player.transform.position.y < -10)
    40.         {
    41.             Player.transform.position += new Vector3(0, 10, 0);
    42.             foreach (GameObject selected in FloatingObjects)
    43.                 selected.transform.position += new Vector3(0, 10, 0);
    44.         }
    45.  
    46.  
    47.         // Z
    48.         if (Player.transform.position.z > 10)
    49.         {
    50.             Player.transform.position += new Vector3(0, 0, -10);
    51.             foreach (GameObject selected in FloatingObjects)
    52.                 selected.transform.position += new Vector3(0, 0, -10);
    53.         }
    54.         else if (Player.transform.position.z < -10)
    55.         {
    56.             Player.transform.position += new Vector3(0, 0, 10);
    57.             foreach (GameObject selected in FloatingObjects)
    58.                 selected.transform.position += new Vector3(0, 0, 10);
    59.         }
    60.     }
    61. }
    62.  
    I get that it's not the most human-readable, but it works.
    Feedback of any bugs is appreciated.
     
    Last edited: Sep 22, 2022
    mopthrow likes this.
  2. AtlasT2022

    AtlasT2022

    Joined:
    Mar 12, 2022
    Posts:
    60
    Though you can't move too fast because it won't be able to keep up.
     
  3. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Thanks for sharing. I think it's a lot weta than your previous one.