2D simple Jumping - GetButton too fast?

Hi all,

I’m starting a new 2D platform project and can’t figure out the simplest thing of all already!

I just want my character to basically jump when I press the space using the “jump” key. It work but looks like the space bar is detected a bunch of time and actually push my character at random height in the air.

Here’s my code which from what I can see over the internet is pretty basic for this kind of things:

Vector3 PlayerBottomLeft = newVector3(-extents.x,-extents.y,0f);
Vector3 rayOrigin = transform.position+PlayerBottomLeft;

grounded=Physics2D.Raycast(rayOrigin,Vector3.down,0.3f,whatIsGround);

//Jumping controls
bool jump=Input.GetButton("Jump");
if(jump)){
     if(grounded){
          rBody2D.AddForce(newVector2(0,jumpForce),ForceMode2D.Impulse);
     }
}

Thanks for any help or pointing out what I do wrong…

Use GetButtonDown.

–Eric

3 Likes

My god I feel Stupid now. I get the difference between the two now.

Thanks to you. I was thinking my colliders detection was wrong but I was far from the truth.