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

Jump Physics in FixedUpdate


After some research, I found that it was better to put all physics related code into FixedUpdate by saving input in a variable of bigger scope.

Although, for a single force (like a jump when a key is pressed), would I need to store the state of the jump keybind in Update and use it in FixedUpdate? Here is an example, I know my explaining is bad:

private bool jumpKey;

private void Update()
{
    jumpKey = Input.GetKey(jumpKeyBind);
}

private void FixedUpdate()
{
    if (jumpKey && other requirements)
        apply the impulse to the rb
}

This code does work, but it still raises [to me] the following question : does every single bit of physics need to be in the FixedUpdate method? If I want to execute the AddForce method when the player reaches somewhere like the top of a stairway, does it need to be in FixedUpdate?

I would also like to point out that the methods GetKeyDown and GetKeyUp don't work in FixedUpdate, see this post


Solution

  • There are many reasons for that. You can find all of them in the Unity Documents. For example, Unity's physics engine is powered by PhysX. All physics calculations are performed in Fixed Timestep. The default Fixed Timestep is "0.02". So when you use your physics operations (like "AddForce()") in "FixedUpdate()", they become more stable, synchronized, and accurate. And this is just one. As I mentioned, there are many reasons for that...