Fall when releasing Jumpkey

I have a code when I release the Space key, I want the player to immediately fall to the ground. Similar to how Hollow Knight works when you release Space, the character falls to the ground mid-air.

At the moment, the code works. My player does fall when I release the space key exactly how I want it.

if (Input.GetKeyUp(KeyCode.Space))
        {
            rbo.velocity = new Vector2(rbo.velocity.x, jumpTimeCounter);

else if (rbo.velocity.y < .1f)     
        {
           // No idea what to type here
        }

But when my player is falling on the (rbo.velocity.y < .1f), I can spam the space key and my player kinda stutters while in the air, how can I fix this in the best possible way?

Like, if there’s a way to disable the GetKeyUp(KeyCode.Space while my player is falling, or something?

I suppose an easy (maybe not the best) fix is to create a bool named something like “canFallDown” that only allows you to press the Fall button once mid-air and returns true when they become grounded again. Basically, bool is true when they are in the air and only when they havent pressed the space, you press space to fall down and bool is false until they hit the ground.

EDIT: I fixed the issue, I had to put falling = true on (rbo.velocity.y < .1f) :slight_smile:

ACTUALLY FIXED IT. Thanks, thats exactly what I did!

void Update()
    {
        if (grounded == true)
        {
            falling = false;
        }
        if (falling == false && Input.GetKeyUp(KeyCode.Space))
        {
            falling = true;
            rbo.velocity = new Vector2(rbo.velocity.x, jumpTimeCounter);
      
        }else if (rbo.velocity.y < .1f && grounded == true && Input.GetKeyUp(KeyCode.Space))
        {
            falling = false;
        }

Im surprised I made it work, since I’ve only been learning Coding and Unity for about 3 weeks, the only issue now is that when I leave a platform and fall from it without jumping, I can press space in the air once only. I need to solve that, its not a huge problem but any ideas for that?

Isnt that what you want? To be able to press space once in the air to fall, not multiple times?

Sorry if it was confusing, the problem I had was if I held down the space bar to jump, and once my player started to fall, if I then released space on the way down it would make a slight jump on the way down. What I wanted to have is my player to fall on the initial jump on “GetKeyUp” and then on the way down completely cancel the button so there’s no awkward stutters in the jump.

It’s all fixed now, with the bools and isGrounded together, I made it work :slight_smile:

GIF of the problem i had
https://gyazo.com/72f58861c08bdd4153ad0399d54038e3

The fix, now it’s much more controllable
https://gyazo.com/f2548d23d5f2a436bb0ee3c2d6e9e77e

1 Like