Search Unity

Question Physics Slowing Down

Discussion in 'Editor & General Support' started by Turquioii, Apr 14, 2023.

  1. Turquioii

    Turquioii

    Joined:
    Apr 12, 2023
    Posts:
    1
    Hello! I am relatively new to Unity. So far it's been going great! I've been able to work on projects with consistent ease and hardly any issues. Except for this one. I'm making a project mostly oriented around physics and things like so. I have three active objects, so there shouldn't be any performance issues. In the Unity editor, the speed is how I like it, but when compiled, it slows down to unfathomably low speeds. How do I fix this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    By far the most likely reason is that you have written code which is framerate dependent.

    In other words - your code behaves differently at different framerates. Typical reasons for this would be:
    • Not using Time.deltaTime when you should be.
    • Using Time.deltaTime when you should NOT be.
    • Doing physics in Update rather than FixedUpdate
    Without seeing your code it's hard to say exactly what's wrong, but likely something in that domain.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    If you are doing a lot of stuff in FixedUpdate() you can reach a point where the fixed timing loops "saturate" out and frame rate will plunge.

    It is a very unstable suddenly-goes-to-10fps effect, something I witnessed when I processed my own particle system pieces and inadvertently hooked them into my FixedUpdate() loop... everything ran fine until the moment that task reached 1/50th of a second, and then the actual Update() rate dropped massively, almost to a slideshow.

    Generally only do things in Update(); except for directly-physics-facing stuff, which should go in FixedUpdate();

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Two good discussions on Update() vs FixedUpdate() timing:

    https://jacksondunstan.com/articles/4824

    https://johnaustin.io/articles/2019/fix-your-unity-timestep

    Beyond that, break out the profiler (Window -> Analyze -> Profiler) and track down what's going on.