Search Unity

How to control time management to sync with custom physics simulation?

Discussion in 'Scripting' started by OFFICIAL_bryanw, Dec 12, 2018.

  1. OFFICIAL_bryanw

    OFFICIAL_bryanw

    Joined:
    Jul 6, 2018
    Posts:
    36
    Hi All,

    I have a simulation I am running that runs slower than real time. I would like to be able to use existing tools that use the existing time management system for animating the motion of different things in the scene.

    I want to be able to stop time completely and then upon completion of my simulation set the Time.deltaTime so that on the next call to my physics simulation, all of the animations are stepped forward only by the amount of time I want.

    I'm simulating a sensor that takes a fixed amount of time to take a measurement, and I'd like to step the scene animations forward by that fixed amount of time.

    Does anyone have ideas on how to accomplish this? Right now I have my own simulation time and some simple animations that I can sync using that simulation time. I'd like to use animations from the asset store but it is difficult to keep them in sync with my simulation (using my current sync method)

    Cheers,
    Bryan
     
  2. OFFICIAL_bryanw

    OFFICIAL_bryanw

    Joined:
    Jul 6, 2018
    Posts:
    36
    One idea I've had is to use the Time.captureFrameRate to control the time, but it's unclear to me how to modify the physics fixed time step to make it so that rigid bodies have the correct physics behavior.
     
  3. OFFICIAL_bryanw

    OFFICIAL_bryanw

    Joined:
    Jul 6, 2018
    Posts:
    36
    Here is what I'm working with now, and it's unclear to me if it's behaving properly... Does the code I have here make sense?

    Code (CSharp):
    1. public class TimeControl : MonoBehaviour
    2. {
    3.     [Range(0.0f, 100.0f)]
    4.     public float timeScale = 1.0f;
    5.  
    6.     public int frameRate = 30;
    7.  
    8.     void Update()
    9.     {
    10.         float fixedTime = 0.02f;
    11.         if (frameRate > 0)
    12.             fixedTime = 1 / frameRate;
    13.  
    14.         Time.captureFramerate = frameRate;
    15.         Time.timeScale = timeScale;
    16.         Time.fixedDeltaTime = timeScale * fixedTime;
    17.     }
    18. }