1

I created a small 2D sidescroller movement.

private Rigidbody2D rigid;
private BoxCollider2D playerCollider;
private bool isMovingRight = false;
private Vector2 movement;
private bool jumpPressed;

private const int MOVEMENT_SPEED = 5;
private const int JUMP_POWER = 5;
private const float GROUNDCHECK_TOLERANCE_SIDE = 0.05f;
private const float GROUNDCHECK_TOLERANCE_BOTTOM = 0.05f;

private void Start()
{
    rigid = GetComponent<Rigidbody2D>();
    playerCollider = GetComponent<BoxCollider2D>();
}

private void Update()
{
    SetMovement();
}

private void FixedUpdate()
{
    Move();
}

private void SetMovement()
{
    float horizontalMovement = Input.GetAxis("horizontal") * MOVEMENT_SPEED;

    if (Input.GetButtonDown("Jump"))
    {
        jumpPressed = true;
    }

    movement = new Vector2(horizontalMovement, rigid.velocity.y);
}

private void Move()
{
    if (GroundCheck(true) || GroundCheck(false))
    {
        if (jumpPressed)
        {
            movement.y = JUMP_POWER;
            jumpPressed = false;
        }
    }

    rigid.velocity = movement;
}

private bool GroundCheck(bool checkLeftSide)
{
    Bounds colliderBounds = playerCollider.bounds;
    Vector2 rayPosition = colliderBounds.center;

    float horizontalRayPosition = colliderBounds.extents.x + GROUNDCHECK_TOLERANCE_SIDE;

    if (checkLeftSide)
    {
        rayPosition.x -= horizontalRayPosition;
    }
    else
    {
        rayPosition.x += horizontalRayPosition;
    }

    return Physics2D.Raycast(rayPosition, Vector2.down, (playerCollider.size.y / 2) + GROUNDCHECK_TOLERANCE_BOTTOM);
}

I register Inputs in Update and handle the Physics in FixedUpdate. When pressing the Jump Button the players jump works fine.

But when pressing jump multiple times, the player jumps up in the air, comes down and jumps one time again.

So if pressing the button more than 1 time the player will jump a second time after finishing the first jump.

How can I avoid this behaviour?

4
  • This line of code movement.y = JUMP_POWER; should probably be movement.y += JUMP_POWER; Commented Feb 14, 2018 at 17:50
  • It's not clear what's your purpose: do you want to completely disable jumps while the character is already jumping? Or add a second jump to the first before landing, Metroid style? Commented Feb 14, 2018 at 18:05
  • 1
    if your groundcheck fails, set jumpPressed = false. Commented Feb 14, 2018 at 18:07
  • 1
    Your logic is "Press Jump? ok... Am I on the ground? Yes, jump set Jump to false. No, I'm not on the ground? do nothing." You should only ever be able to jump if you are grounded so if your groundchecks both fail use an else to set jumpPressed false. Commented Feb 14, 2018 at 18:09

1 Answer 1

2

Using your code, I added 2 comments and an else statement to clarify what I mean in my comments. This should fix your issue.

private void SetMovement()
{
    float horizontalMovement = Input.GetAxis("horizontal") * MOVEMENT_SPEED;

    if (Input.GetButtonDown("Jump"))
    {
        // Only ever gets set to false if you are grounded and it makes you jump.  
        // Should only be set to true if you can jump.  So you could add your groundChecks here... 
        // or use the method I am showing
        jumpPressed = true;   
    }

    movement = new Vector2(horizontalMovement, rigid.velocity.y);
}

private void Move()
{
    if (GroundCheck(true) || GroundCheck(false))
    {
        if (jumpPressed)
        {
            movement.y = JUMP_POWER; 
            // This was the only place this every was set to false, and 
            // specific conditions were required to make this happen.
            jumpPressed = false; 
        }
    }
    // fastest way to test
    else 
    {
         // yes they pressed jump but they can't jump so reset it, now 
         // they wont jump as soon as they land. just because they got 
         // trigger happy.
         jumpPressed = false; 
    }

    rigid.velocity = movement;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.