#Jump Code Syntax

1 messages · Page 1 of 1 (latest)

silk leaf
#

Hi, just making this thread so I don't interrupt the other person asking for help!

I fixed the jump, thank you guys so much for the advice! Mostly I'm just coming back because I'm just not sure if I did it in a very good way. Turns out it was calling the coroutine twice because you are grounded through 2 fixed updates, and the way I fixed that was by adding a "isJumping" bool that activates here:

        if (Input.GetKey(KeyCode.Space) && isGrounded && !isJumping) 
        {
            Debug.Log("Blast off!");
            isJumping = true;
            StartCoroutine(Jump());
        }```

and turns off here:
```c#
    private IEnumerator Jump() {
        //body.AddForce(new Vector2(0, jumpHeight*5), ForceMode2D.Impulse);
            
        float cooldown1 = 1;
        while (Input.GetKey(KeyCode.Space) && cooldown1 > 0) {
            body.velocity = new Vector2(body.velocity.x, 10 + -3*(cooldown1 - 1)); //.998 - 1 = -.002

            cooldown1 -= Time.deltaTime;
            yield return null;
        }
        isJumping = false;
    }```

Is this the most elegant solution, or is there a better way to prevent the second execution that i'm just not seeing? By the way, thank you guys so much for mentioning the issue with the yield outside the while loop; that entirely fixed the issue with the variable jump length not working, I just need to tune the values now!

To be clear, everything works now, this is just asking what would be the best style/approach/syntax for this problem so my code is cleaner and causes less problems later. If there is a better solution that will just require some research, just throw a link down :)

P.S.: This is an unrelated question, but I read somewhere that it's better to have Inputs within Update() and any physics-based movement within FixedUpdate(). Should I refactor my code so that's how it works, or is it more or less negligible?
sturdy veldt
#

you would not even have to have the coroutine tbh.

severe jewel
#

in general you should have inputs in update yes

sturdy veldt
#
body.velocity = new Vector2(body.velocity.x, 10 + -3*(cooldown1 - 1)); //.998 - 1 = -.002

            cooldown1 -= Time.deltaTime;

could also just be inside your holding the key update statement

severe jewel
#

is this a platformer or something?

sturdy veldt
#

only thing you have to exclude from update is the "isJumping" boolean to turn on and not reset in update

severe jewel
#

setting the velocity throughout the cooldown time could cause issues with going through thin ceilings

silk leaf
severe jewel
#

might want to consider doing AddForce here instead of setting the velocity

silk leaf
severe jewel
#

not sure how that's relevant

silk leaf
sturdy veldt
#

does not need to be while, can also be if 😉

severe jewel
#

well with this implemenation you need a once-per-frame loop

Update is a once-per-frame loop

sturdy veldt
#

you have that if statement already for spacebar, just add the cooldown as third argument for example and you should be able to remove the while loop entirely