Character Jumping code is Glitchy

Hey, I’m making my own 2.5d platformer and I’m making the character controller currently. The jumping for my character is currently a bit glitchy. Sometimes it won’t jump, other times you can double jump (you shouldn’t be able to double jump!). Here is my code: ` isGrounded = doGroundCheck();

    if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    {
        isJumping = true;
        jumpTimeCounter = jumpTime;
        rigidBody.velocity = Vector2.up * jumpForce;
    }

    if (Input.GetKey(KeyCode.Space) && isJumping == true)
    {
        if (jumpTimeCounter >= 0)
        {
            rigidBody.velocity = Vector2.up * jumpForce;
            jumpTimeCounter -= Time.fixedDeltaTime;
        }
        else
        {
            isJumping = false;
        }
    }

    if (Input.GetKeyUp(KeyCode.Space))
    {
        jumpTimeCounter = 0;
        isJumping = false;
    }`

Sorry if I don’t respond soon. Thanks if anyone can help make this work better!

1 Like

Hi,

You could add a bunch of Debug.Logs with meaningful messages to the code to see whether the values are as expected.

How is the DoGroundCheck() function working?

I will certainly try that!

It used Physics.CheckSphere() at the position of his feet to determine if there is ground there or not. The ground also has a ground layer mask to determine if it is ground or not. :grinning:

I think it’s possible the problem is in:
rigidBody.velocity = Vector2.up * jumpForce;
If you have any other codes that change the velocity it might kill your jump. What I mean is this code it telling the player to move up, but then is overridden when you move left or right.

Maybe a solution would be:

rigidBody.velocity = new Vector2( rigidBody.velocity.x , jumpForce );

And also adjust your other codes that move the player.

Thank you, I did change it buuut… It didn’t work. I can still double jump sometimes and I can’t Jump at all other times. I tried using debugs but everything happens to fast in a Jump and I don’t know how to reproduce the bug exactly so I just don’t know when I can double Jump or what I did to make it not work for Jumping at all. Am I able to send the project somehow or maybe you have another solution?

code for 2d jump is very simple

public void Jump()
{
   if (Input.GetKeyDown(KeyCode.Space) )
    {
            if(rigidbody.GetComponent<Collider2D>().isTouchingLayers)
           {
                  rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce) ;
           }
    }
}

This topic was automatically closed after 16 hours. New replies are no longer allowed.