Search Unity

Floating origin doesn't work on the Y axis

Discussion in 'Physics' started by Cliftonm, Mar 11, 2016.

  1. Cliftonm

    Cliftonm

    Joined:
    Nov 15, 2015
    Posts:
    36
    I'm using the floating origin script on the wiki, but it only works on the X axis and Z axis. I've tried several different modifications to it, but it still won't work on the Y axis (Up and down.) Any ideas?
    Script in case you want to stay on the page:
    Code (CSharp):
    1. // FloatingOrigin.cs
    2. // Written by Peter Stirling
    3. // 11 November 2010
    4. // Uploaded to Unify Community Wiki on 11 November 2010
    5. // Updated to Unity 5.x particle system by Tony Lovell 14 January, 2016
    6. // URL: http://wiki.unity3d.com/index.php/Floating_Origin
    7. using UnityEngine;
    8. using System.Collections;
    9. [RequireComponent(typeof(Camera))]
    10. public class FloatingOrigin : MonoBehaviour
    11. {
    12.     public float threshold = 100.0f;
    13.     public float physicsThreshold = 1000.0f; // Set to zero to disable
    14.     #if OLD_PHYSICS
    15.     public float defaultSleepVelocity = 0.14f;
    16.     public float defaultAngularVelocity = 0.14f;
    17.     #else
    18.     public float defaultSleepThreshold = 0.14f;
    19.     #endif
    20.     ParticleSystem.Particle[] parts = null;
    21.     void LateUpdate()
    22.     {
    23.         Vector3 cameraPosition = gameObject.transform.position;
    24.         cameraPosition.y = 0f;
    25.         if (cameraPosition.magnitude > threshold)
    26.         {
    27.             Object[] objects = FindObjectsOfType(typeof(Transform));
    28.             foreach(Object o in objects)
    29.             {
    30.                 Transform t = (Transform)o;
    31.                 if (t.parent == null)
    32.                 {
    33.                     t.position -= cameraPosition;
    34.                 }
    35.             }
    36.             #if SUPPORT_OLD_PARTICLE_SYSTEM
    37.             // move active particles from old Unity particle system that are active in world space
    38.             objects = FindObjectsOfType(typeof(ParticleEmitter));
    39.             foreach (Object o in objects)
    40.             {
    41.                 ParticleEmitter pe = (ParticleEmitter)o;
    42.                 // if the particle is not in world space, the logic above should have moved them already
    43.         if (!pe.useWorldSpace)
    44.             continue;
    45.                 Particle[] emitterParticles = pe.particles;
    46.                 for(int i = 0; i < emitterParticles.Length; ++i)
    47.                 {
    48.                     emitterParticles[i].position -= cameraPosition;
    49.                 }
    50.                 pe.particles = emitterParticles;
    51.             }
    52.             #endif
    53.             // new particles... very similar to old version above
    54.             objects = FindObjectsOfType(typeof(ParticleSystem));      
    55.             foreach (UnityEngine.Object o in objects)
    56.             {
    57.                 ParticleSystem sys = (ParticleSystem)o;
    58.                 if (sys.simulationSpace != ParticleSystemSimulationSpace.World)
    59.                     continue;
    60.                 if (parts == null || parts.Length < sys.maxParticles)
    61.                     parts = new ParticleSystem.Particle[sys.maxParticles];
    62.                 int num = sys.GetParticles(parts);
    63.                 for(int i = 0; i < num; ++i)
    64.                 {
    65.                     parts[i].position -= cameraPosition;
    66.                 }
    67.                 sys.SetParticles(parts, num);
    68.             }
    69.             if (physicsThreshold > 0f)
    70.             {
    71.                 float physicsThreshold2 = physicsThreshold * physicsThreshold; // simplify check on threshold
    72.                 objects = FindObjectsOfType(typeof(Rigidbody));
    73.                 foreach (UnityEngine.Object o in objects)
    74.                 {
    75.                     Rigidbody r = (Rigidbody)o;
    76.                     if (r.gameObject.transform.position.sqrMagnitude > physicsThreshold2)
    77.                     {
    78.                         #if OLD_PHYSICS
    79.                         r.sleepAngularVelocity = float.MaxValue;
    80.                         r.sleepVelocity = float.MaxValue;
    81.                         #else
    82.                         r.sleepThreshold = float.MaxValue;
    83.                         #endif
    84.                     }
    85.                     else
    86.                     {
    87.                         #if OLD_PHYSICS
    88.                         r.sleepAngularVelocity = defaultSleepVelocity;
    89.                         r.sleepVelocity = defaultAngularVelocity;
    90.                         #else
    91.                         r.sleepThreshold = defaultSleepThreshold;
    92.                         #endif
    93.                     }
    94.                 }
    95.             }
    96.         }
    97.     }
    98. }
     
    Crossway likes this.
  2. Cliftonm

    Cliftonm

    Joined:
    Nov 15, 2015
    Posts:
    36
    I figured it out. To fix it, simply remove this part:
    Code (CSharp):
    1. cameraPosition.y = 0f;
     
    Crossway likes this.
  3. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,079
    Hey guys, there is a problem with Unity 5.6 and floating origin! It works but I mentioned this script can't remove shaking problem caused by limit of float point anymore!!!!!!

    Please help thank you.
     
  4. orkunsevengil

    orkunsevengil

    Joined:
    Aug 2, 2016
    Posts:
    1
    FixedUpdate
     
    ksam2 likes this.
  5. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,079
    Sorry but what is your mean by this? I can't figure out, please help.

    Edit: You mean I should change LastUpdate to FixedUpdate? But it doesn't helped still have same problem.
     
    Last edited: Apr 28, 2018
  6. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    127
    Anybody has an idea how to deal with static objects? They do NOT get translated. Even temporarily setting the static flag to false, does not help!
     
  7. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    127
    found out it´s "static batching". but the wohle thing does not work even when turned off.
    My suspision, that all occlusion and lightning data gets screwed up, turned out to be more than true: flickering objects all over the place.
    :(
     
  8. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
    Did you manage to figure this out?