how can i make it so the player can hold down jump and only jumps when grounded

I’ve tried doing this but it didnt work out that well and it led to very inconsistent jumps that go super high up, this is my current script for jump input

if (Input.GetKeyDown(jumpKey)&& isGrounded)
        {
            Jump();
            StopSlide();
        }

i tried replacing input.getkeydown with input.getkey but it just doesnt work…help, i dont want to keep on pressing jump ;-;

Change your GetKeyDown() to GetKey().

If that doesn’t work, this one can do it:

Look at the very bottom underneath the code for the edit describing the one change you need to make.

i tried this(just using getkey instead of getbutton) and it leads to very inconsistent jumps with some being very small and some just launching you into the air.

how do i fix this

The code I linked above produces the correct upward velocity on line 78.

To fix your code, identify how you are not doing it that way… then … wait for it. Do it that way! :slight_smile:

Does the code work for Rigidbody?

I think it’s pretty obvious there are no Rigidbodies in the code above, but the formula is the same: it’s just basic dynamics 101. That doesn’t change. Try it now with a rigidbody and a quick test script if you disbelieve me.

If you’re getting irregular velocities, make sure you’re obeying all the proper use of physics systems: only transact in FixedUpdate() against Rigidbodies, etc.

creo que tengo la respuesta aqui tengo este codigo que le permite moverme y saltar tambien que solo salte al toca el suelo pero necesito que me ayudne enotra cosa por alguna razon me dice que me falta un} pero estoy seguro que no me falta ninguno podrian ayudar

usando System.Collections;
utilizando System.Collections.Generic;
usando UnityEngine;

controlador de jugador de clase pública: MonoBehaviour {

bool canJump;

// Se llama al inicio antes de la primera actualización del cuadro
inicio vacío ()
{

}

// La actualización se llama una vez por fotograma
Actualización nula ()
{
if (Input.GetKey (“izquierda”))
{
gameObject.GetComponent () .AddForce (nuevo Vector2 (-20000f * Time.deltaTime, 0));

}

if (Input.GetKey (“derecha”))
{
gameObject.GetComponent () .AddForce (nuevo Vector2 (20000f * Time.deltaTime, 0));

}

if (Input.GetKeyDown ("arriba)) gg canJump; )

{

canJump = falso;
gameObject.GetComponent () .AddForce (nuevo Vector2 (0, 10000f));
}

}

Vacío privado OnCollisionEnter2D (colisión Collision2D)
{
if (collision.transform.tag == “ground”)
{
canJump = troue;
}
}
}

ignoren las cosa en español eso automaticamente las traduce en mi codigo estan en ingles

could you translate this to english? Im not using google so I can’t translate the page or at least i don’t know how to and also could you use code blocks?

https://google.gprivate.com/search.php?videos/search?q=unity+jump&docid=607988526817215985&mid=F920FD3ACCAB2EDBC2B6F920FD3ACCAB2EDBC2B6&view=detail&FORM=VIRE

Im sorry but did you even read the title or the post? My problem is that I can’t jump while HOLDING THE KEY, NOT HOW TO JUMP

All is in that video

NO ITS NOT. the video explains how to do the jump with INPUT.GETKEYDOWN, SO YOU HAVE TO PRESS IT EVERY TIME, IM LOOKING FOR HOW TO HOLD THE JUMP BUTTON AND ONLY JUMP WHEN GROUNDED OMFG IS IT THAT HARD TO UNDERSTAND

YOU SEE THIS?7406405--905444--Screenshot 2021-08-10 132442.png
IT SAYS INPUT.GETKEYDOWN, THAT MEANS YOU HAVE TO PRESS IT AGAIN EVERY TIME

Maybe instead of yelling at them, you could change Input.GetKeyDown to Input.GetKey as that one is detected all the time it is held down. After all, that code already checks if the player is grounded.

1 Like

I do agree with the sentiment here. Please refrain from shouting and putting down fellow forum users if you don’t appreciate the answers you are getting. Just be friendly and understanding. People are trying to help you.