Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics simulation timeline scrubbing.

Discussion in 'Physics' started by username132323232, Sep 5, 2019.

  1. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    477
    I’m working on a video (about 5 minutes long) that is basically a rigid body physics simulation. So there’s no user input, AI, etc. My question: Is there a way to avoid running the entire simulation every time a change is made?

    Example: Change weight of some cube. I would like to see how it affects the simulation at t=5.0 minutes. Do I need to wait 5 minutes to see that?

    As far as I know, this would not be a problem in Blender. After it calculates everything, I could just scrub the timeline and view the simulation at any time right away. I’m pretty sure this kind of functionality is not native to a game engine, but I wonder if something like that is possible in Unity.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Not sure if you mean 2D or 3D physics but it's the same answer but I'll refer to my area which is 2D physics.

    You cannot "scrub" i.e. move back and forwards but you can manually simulate forwards using Physics2D.Simulate. You can also have a PhysicsScene2D per scene to simulate in a sandbox. Performing a simulation for a specific time-delta doesn't mean it takes that long to simulate that time-delta. In most cases it's much less but if the simulation is very very complex then it might take longer.

    It's very important to note however that you cannot just call that simulate step by specifying a time-delta of 5 minutes (300 seconds) as it won't give you the results you want. Simulation is done in small steps, typically the fixed-time-delta so if that were set to 1/50th (sec) then you'd call that 50 times for a 1 second of simulation. Obviously then for 300 seconds of simulation you'd need to call that 50 * 300 times which is a lot of calls but then again so is 5 minutes of simulation. ;)
     
    username132323232 likes this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Yes, there are a couple of ways to do that:
    • Modify Time.timeScale to the time acceleration factor you want. For example, Time.timeScale = 10 means the scene will be playing at 10x the normal speed. However, visual frames (Update) will be likely skipped to achieve that playback rate.
    • Configure Time.captureFrameRate to the frame rate you want for the video. This will play the scene as fast as possible, but assuming that number of frames per second. You may want to capture an screenshot in each Update for getting each generated frame. This effectively will produce the frames for your video as fast as posible.
     
    username132323232 likes this.
  4. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    477
    Thank you for answering my question. This really helps!

    It sounds like it may be possible to add timeline scrubbing functionality by somehow baking physics simulation into an animation. I looked in the asset store, but didn’t find any current assets that would do that. I guess GameObjectRecorder could be useful for this purpose.