#Double Jump Issue

1 messages · Page 1 of 1 (latest)

true dove
#

Hello! I'm currently having an issue with implementing double jump in my game. I'm using the code from this video as a base(because i really like how the jump works:https://youtu.be/vFsJIrm2btU), the current code below is my best attempt at making it, it's buggy and it barely works, I have tried other ways than this one, but they either broke the normal jump or just didn't work at all. Here's the code:

float jumpRemember = 2;
void FixedUpdate(){
    
Vector2 v2GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
        Vector2 v2GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
        bool bGrounded = Physics2D.OverlapBox(v2GroundedBoxCheckPosition, v2GroundedBoxCheckScale, 0, lmWalls);

        fGroundedRemember -= Time.deltaTime;
        if (bGrounded)
        {
            fGroundedRemember = fGroundedRememberTime;
            jumpCount = jumpRemember;
        }

        fJumpPressedRemember -= Time.deltaTime;
        if (Input.GetButtonDown("Jump") && jumpCount != 0)
        {
           
            fJumpPressedRemember = fJumpPressedRememberTime;
           
        }
        

        if (Input.GetButtonUp("Jump") && jumpCount != 0)
        {
            if (rigid.velocity.y > 0)
            {
                rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * fCutJumpHeight);
            }
        }
        
        if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0) || Input.GetButtonDown("Jump") && jumpCount != 0)
        {
            fJumpPressedRemember = 0;
            fGroundedRemember = 0;
          
            rigid.velocity = new Vector2(rigid.velocity.x, fJumpVelocity);
            jumpCount--;
        }
        if(jumpCount <= 0)
        {

            Debug.Log($"Set to zero, soz,{jumpCount}");
          
        }```
quaint matrix
#

What's with the f and v

true dove
#

they were in the tutorial

#

and i didn't really bother changing them

quaint matrix
#

that's a bad naming practice

true dove
#

yeah...

quaint matrix
#

okay so how does the algorithm work?

#

what's the time component?

true dove
#

you mean the groundRememberTime and the other one?

#

they're fixed values

quaint matrix
#

doubt

#

what's fGroundedRemember

true dove
#

i mean, they're defined a certain value and they change overtime

quaint matrix
#

like what behavior is it producing

true dove
#

it's a timer

#

grounded is how much time passed after you left the ground

#

and the jump one how much time passed after yoy pressrf thr jump button

quaint matrix
#

so you can't double jump after a certain time elapses between jump inputs?

#

terrible variable names lol

true dove
#

you can't jump once

true dove
quaint matrix
#

wait ignore the code for a sec. tell us what kind of double jump you want

true dove
#

well you jump once, you can jump again while you're in the air

#

i don't really care about the timers since they don't matter

#

because they're only required for the initial jump

#

ideally i want the double jump to be more powerful than the first one

#

but at this point, I'm just fine with a plain old one

quaint matrix
#

alright I'm glad I asked what u wanted

true dove
#

fGroundedRemember and FJumpPressedRemember are timers

calm forum
#

I think I should start by pointing out that you should use Time.fixedDeltaTime in the FixedUpdate function.
Basically, when you jump and are grounded you have to set the jumpCount, when you are in the air you want to jump again and decrease the jumpCount.

#
if (Input.GetButtonDown("Jump") && jumpCount != 0)    //Add && bGrounded
        {
           
            fJumpPressedRemember = fJumpPressedRememberTime;
           
        }

if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0) || Input.GetButtonDown("Jump") && jumpCount != 0)     //Add &&!bGrounded
        {
            fJumpPressedRemember = 0;
            fGroundedRemember = 0;
          
            rigid.velocity = new Vector2(rigid.velocity.x, fJumpVelocity);
            jumpCount--;
        }
quaint matrix
#

I think throw away the code

#

It's kinda spaghet

true dove
#

the code is like 4 years old

quaint matrix
#

it's not the age that's bothering me

#

and also if you're not even using timers, you can structure it differently

true dove
#

for the 2nd jump, i won't be needing them

#

maybe the reason why i can't modify it easily is because it's meant for a single jump

#

but anyway, I think I've figured it out how to solve it, I'll try vlad's suggestion once i come back home

#

since replying on the bus is really not that ideal 😅

#

but thank you both either way

calm forum
#

You're welcome! And I hope it works

quaint matrix
#

No problem

true dove
#

None of the suggestions work, guess I'll have to implement a different way to enable coyote jumping and a jump buffer

true dove
#

K, found another source where i shamelessly copied the code

#

it works, somehow

quaint matrix
#

this is not a good way of coding lol

true dove
#

ye

#

the code is basically the same as the above but with more spaghetti and a bool that tracks whether you can double jump

#

so i was supposed to use a bool just to solve a specific issue, which I'm kinda sad about

#

I'll make sure to dissect the code, if that helps me learn anything about it

#

i swear ive tried doing it with a bool and it not working at all

true dove
#

here's the code i nabbed if anybody's wondering:

if (bGrounded)
        {
            fGroundedRemember= FGroundedRememberTime;
        }
        else
        {
            fGroundedRemember -= Time.fixedDeltaTime;
        }

        if (Input.GetButtonDown("Jump"))
        {
            fJumpPressedRemember = fJumpPressedRememberTime;
        }
        else
        {
            jumpPressedRemember -= Time.fixedDeltaTime;
        }

        if (bGrounded && !Input.GetButton("Jump"))
        {
            doubleJump = false;
        }

        if (fJumpPressedRemember > 0f)
        {
            if (fGroundedRemember > 0f || doubleJump)
            {
                rb.velocity = new Vector2(rb.velocity.x, fJumpVelocity);

                fJumpPressedRemember = 0f;

                doubleJump = !doubleJump;
            }
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);

            fGroundedRemember = 0f;
        }

        
quaint matrix
#

so many things wrong I wish I didn't see the code

true dove
#

Yeah

#

i just realized that the code is buggy af

#

you can spam the jump button and it breaks it entirely

#

good thing i tested that

#

and I'm frustrated since I'll have to either scrap my project entirely, or just bash my head trying to figure out how to implement a double jump

quaint matrix
#

lol why is scraping the project even an option you're considering

true dove
#

because without the feature, I'm kinda just making the same project I made 4 months ago

#

and I've kind of designed some levels with the feature in mind

#

and because I'm stubborn af, so I'll probably keep bashing my head trying to implement it

#

wait

#

did i just... figure it out

#

there is no way

#

lmao what

#

how

#

how did implementing a jump counter fix everything

#

there's no fucking way it was that incredibly easy to fix

#

bruh

#

k issue solved hopefully