Search code examples
c#unity-game-enginegame-physics

Unity - Adjusting Time.fixedDeltaTime for smooth Slow mo messes up Addforce


I'm slowing down time in my game using the two lines:

Time.timeScale = slowMotionSpeed;
Time.fixedDeltaTime = slowMotionSpeed * 0.02f;

and the slow motion works just fine, it is smooth just as it has to be.

The problem is though that I have a lot of AddForce utilities on the levels, like jumping, trampolines, wind etc and they are calculated so that the player needs very precise timing to get that right.

I noticed that after using it in slow motion instead of regular speed those trampolines don't take me far enough to make the jump no matter how many times I try (and it was an easy jump before that).

If I try the jump in slow motion, but without adjusting the Time.fixedDeltaTime, the game lags, but the jump is successful.

The question is, how to address this so that AddForce works fine and throws the player to the same distance, even if the Time.fixedDeltaTime is modified?

Edit:

the trampoline:

float jumpForceAdded;
void OnTriggerStay2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Player") && !CharacterController2D.movementDisabled && !CharacterController2D.playerDied)
    {
        Rigidbody2D otherRB = other.gameObject.GetComponent<Rigidbody2D>();
        if (JumpInput.JUMP)
        {
            otherRB.AddForce(new Vector2(0,jumpForceAdded * 2f));
        }
        else
        {
            otherRB.AddForce(new Vector2(0,jumpForceAdded));
        }
        StartCoroutine(ChangeSpritesBack());
    }
}

It uses "double jump" if we jump on the trampoline, and just a regular jump if we don't.

Edit again: I added the Addforce version which I should have added on the beginning. I used a deprecated one, sorry for the inconvenience.

By the way, as I'm using OnTriggerStay2D, my guess is that the force itself isn't different - but the duration while the player receives that force is. I don't know if proportionally changing the force based on time would work in all cases.


Solution

  • When you modify Time.timeScale, you do not have to modify Time.fixedDeltaTime. The reason your game appears to "lag" is because your rigidbodies are not interpolated. Set Rigidbody.interpolation to RigidbodyInterpolation.Interpolate.