0
Answered

Physics when time paused

RodMath 2 months ago • updated by Lazlo Bonin (Lead Developer) 3 weeks ago 1

Hi, 

I've been messing around with adding timelines to a rigidbody and have a couple of questions.  Apologies if it is in the docs or forums but I couldn't find it:

1. It seems that if I pause time, move a rigidbody then un-pause time, it will revert back to its position when time was paused.  I.e. set timescale to 0 with a rigidbody at (0,0,0), move it it (10, 10, 10), then set timescale to 1, the rigidbody reverts back to (0,0,0). Is this intended behaviour?   

2. Likewise, when time is paused, if I apply forces, nothing happens.  I was expecting the force to be applied when time restarted?

I mainly ask because it took me a while of debugging to figure out these were happening.  As a workaround, instead of pausing I'm setting time to ultra-slow-mo, but is there a better way?

Thanks

Rod

Chronos Version:
2.4.14
Unity Version:
2018.2.12f1
Answered

Hi RodMath,

Both of these issues are unfortunately by design. #1 is to ensure consistency when we cross the zero threshold, and #2 is because, well, Chronos doesn't know you intend to delay forces and wouldn't know what to do if you started, say, rewinding. 

For #1, you can try commenting line 59 of RigidbodyTimeline.cs. There may be side effects though, let me know how it goes!

For #2, I would recommend creating your own list of delayed forces to add, then using the OnStopPause event to apply them. Something like:

List<Vector3> pendingForces = new List<Vector3>();

void AddForce(Vector3 force)
{
    if (timeline.timeScale == 0)
    {
        pendingForces.Add(force);
    }
    else
    {
        rigidbody.AddForce(force);
    }
}

void OnStopPause()
{
    foreach (var force in pendingForces)
    {
        rigidbody.AddForce(force);
    }

    pendingForces.Clear();
}