Why is force not being applied to my rigidbody

Here is my code

 void Update()
    {
        if (isGrounded()){
            coyoteTimer = maxCoyoteTimer;
        }
     

        if (Input.GetKeyDown(KeyCode.Space)){
            jumpBuffer = maxJumpBuffer;
        }

       

       

        if (coyoteTimer>0) coyoteTimer -= Time.deltaTime;
        if (jumpBuffer>0) jumpBuffer -= Time.deltaTime;
        
        Jump();



void Jump(){
        if (coyoteTimer>0 && jumpBuffer>0){
            jumpBuffer = 0;
            coyoteTimer = 0;
            Debug.Log("jumped");
            rb.AddRelativeForce(jumpForce * Vector2.up,ForceMode2D.Impulse);
            
            
            

        }
    }

It works perfectly when the character is on ground but the jump buffer doesnt work. It even prints ā€œjumpedā€ but the force is not applied

What is the jump buffer supposed to do? From what I can see here, it’s not going to do anything. It will be set as soon as the player presses space and - provided maxJumpBuffer is greater than Time.deltaTime - it will do the jump and be reset again. It may as well have been a boolean.

But you say you get the ā€˜jumped’ message, which means it was meant to jump but it didn’t. What is jumpForce set to?

The jump buffer is there so that even if the player presses the jump a few miliseconds before touching the ground it registers it and jumps WHEN it touches the ground.

Here’s how its supposed to work
Suppose the character is in the air and is about to the touch the ground in just 0.5 seconds. Now assume the player presses the jump button. Let the maxJumpBuffer be say 1 sec. So when the player presses the jump button, the jumpBuffer sets to 1 and since the player was about to the touch the ground in 0.5 sec, when it touches the ground the jumpBuffer will be (maxJumpBuffer-0.5) ie >0.
And when he touches the ground, the coyoteTimer will also be set to >0. And since both the conditions are met he should jump.

Yes the jumped message is seen. And the jumpForce is high enough. Plus the player jumps perfectly when he is on the ground.

OK, I understand. All good.

I believe the problem is that you have velocity of the character falling (at the end of the jump) that needs to be overcome by the new jump force. It’s not enough. You may need to completely remove the current velocity before adding the new jump velocity.

void Jump()
{
    if (coyoteTimer > 0 && jumpBuffer > 0)
    {
        jumpBuffer = 0;
        coyoteTimer = 0;
        Debug.Log("jumped");
        rb.velocity = Vector3.zero; // reset the current velocity
        rb.AddRelativeForce(jumpForce * Vector2.up, ForceMode2D.Impulse);
    }
}

But this is not the right way to do it. You want the physics to handle velocity changes. So, perhaps grab the current y-value from velocity and add it to the jump force. It will be downwards (negative) so you’d need to invert it

void Jump()
{
    if (coyoteTimer > 0 && jumpBuffer > 0)
    {
        jumpBuffer = 0;
        coyoteTimer = 0;
        Debug.Log("jumped");
        float currentVelocityY =  rb.velocity.y;
        rb.AddRelativeForce((-currentVelocityY + jumpForce) * Vector2.up, ForceMode2D.Impulse);
    }
}

Now you add enough force to overcome the current downward velocity and perform the jump

Thanks a lot. That worked perfectly

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.