AddForce with Delay Coroutine

Hello,

I want a delay coroutine before to run my addforce jump because I have a pre-animation, but this break my addforce impulse.

I have test lot of configuration script, but always a bug

void update {
        if (jumpBufferCounter > 0 && coyoteTimeCounter > 0)
        {
            StartCoroutine(JumpDo());
        }
}

IEnumerator JumpDo()
    {
        pState.pre_jump = true;
        anim.SetBool("Jumping", true);
        yield return new WaitForSeconds(0.07f);
        pState.jumping = true;
        pState.pre_jump = false;
        JumpForce();       
    }

    void JumpForce()
    {
        float force = pDataM.jumpForce;
 if (rb.velocity.y < 0)
        {
            force -= rb.velocity.y;
        }
        rb.AddForce(Vector2.up * force, ForceMode2D.Impulse);
    }

the problem is that by adding a delay to addforce my impulse becomes too great

if I remove the delay and the coroutine and run my animation script and the script with addforce at the same time, no more worries, but this is not the desired result

Thanks for the help.

It seems to me like you are calling the JumpDo coroutine multiple times, I can’t see you resetting the values for jumpBufferCounter and coyoteTimeCounter in your script. If those values are not ressetted so that update can’t call the StartCoroutine it will keep adding force. Atleast that is what i see form the code. After Starting the coroutine you can just set jumpBufferCounter and coyoteTimeCounter to 0. Let me know if it solves your problem.

instead of cooroutine you could add a key event in the animation so that when the animation ends you do the jump

For the coyote and buffer, I have this script too.

Sorry for not mentioning it, I didn’t think that would be the problem

    private int jumpBufferCounter = 0;
   private int jumpBufferFrames = 8;
    private float coyoteTimeCounter = 0; 
    private float coyoteTime=0.1f;

void update(){
UpdateJumpVariables();
}

void UpdateJumpVariables()
    {
        if (Grounded())
        {
            pState.jumping = false;
           // pState.falling = false;
            coyoteTimeCounter = coyoteTime;
           // anim.SetBool("Falling", false);
        }
        else
        {
            coyoteTimeCounter -= Time.deltaTime;

        }

        if (jumpButtonDown)
        {
            jumpBufferCounter = jumpBufferFrames;

        }
        else
        {
            jumpBufferCounter--;
        }

I finally solved the problem with @Archroda method. There was actually another conflict with my double jump and the coyote (on code not shown here), so I simply added an “else” if between my code launching the jump and the double jump instead of ifs separated and I have add a condition in my double jump:

&& coyoteTimeCounter <= 0

Then the delay added to the jump prevented the cut jump from reducing the jump when you release the jump button, so I added a condition to the jump to resolve this problem:

      else if (jumpBufferCounter > 0 && coyoteTimeCounter > 0 && !jumpButtonUp)
        {
            StartCoroutine(JumpDo());
        }

Finally you have to set the jumpBufferCounter = 0; at the start of the IEnumerator launching the animation, but coyoteTimeCounter = 0; can be put directly into the ForceJump() function

Normally everything works now (at least I hope ^^)

Thank you, it allowed me to understand that the problem did not come from AddForce directly but from before in my code (coyote and buffer). I wasn’t looking in the right place.