Time.fixedDeltaTime decreases jump force

i tried adding slow motion to my game using Time.timeScale but then it lagged my game, so i looked it up and to fix that issue i needed to add “Time.fixedDeltaTime = Time.timeScale * 0.02f;” but after i added that it decreased my jump force.

slow mo code

public class SlowMotionScript : MonoBehaviour
{
    public float SlowMo;

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            Time.timeScale = SlowMo;
            Time.fixedDeltaTime = Time.timeScale * 0.02f;
        }

        if(Input.GetKeyUp(KeyCode.LeftShift))
        {
            Time.timeScale = 1;
            Time.fixedDeltaTime = Time.timeScale * 0.02f; //Jump force goes back to normal here
        }
    }
}

It’s probably not really appropriate to be factoring Time.fixedDeltaTime into your jump impulse force.

1 Like

Are you multiplying your jump force by fixedDeltaTime somewhere? Normally a jump force would be a 1 shot thing, so you wouldn’t want to change the amount of forced based on any delta time.

no i didn’t change anything anywhere i just made this script and it decreased my jump force whenever i hold g

Please show the code where you apply the jump force.

in Player GameObject

void Update()
{
    isGrounded = GetComponent<Rigidbody2D>().isTouching(Floor.GetComponent<Collider2D>());

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
            rb.AddForce(new Vector2(0f, 900f));
    }
}

The default Force mode for AddForce is ForceMode.Force which takes fixedDeltaTime into account. For an instant force like this you’ll want to be using ForceMode.Impulse.

AddForce(myForce, ForceMode.Impulse);

You will likewise have to adjust the force magnitude down by around 50x, but it will now be independent of your time scale

2 Likes

that fixed it, thanks