- Home /
[Unity2D Platformer] - Why is my player Double Jumping?
Hey! I'm trying to implement a jump mechanic where the longer the player presses the Jump button he will jump higher (to a certain amount). It is working correctly when I keep the button pressed. however if in mid-jump I release the jump key and press it again I am able to double jump. I don't want the player to double jump, what am I doing wrong?
I think it has to do with how I am calculating the inputs from the player. It is the first time I am trying to separate the Input methods from the physics methods itself as I learned that is better to write physics methods in Fixed Update while keeping inputs in Update.
however, I can't understand why my player is about to jump again while he is not grounded. I already debugged my isGrounded bool in the inspector and indeed after leaving the ground, I am not grounded anymore. This is really weird I'm breaking my head around this. Please help me, Thanks a lot, Eden.
private void Update()
{
Inputs();
CollisionChecks();
AnimationSetup();
}
private void FixedUpdate()
{
PlayerMovement();
PlayerJump();
HandleDash();
WallSlide();
WallJump();
}
private void Inputs()
{
//Horizontal Inputs
xMoveInput = Input.GetAxisRaw("Horizontal"); //GetAxisRaw meaning snappy movement, remove raw for fluidity
//Jump Inputs
if (Input.GetButtonDown("Jump"))
{
isJumpButtonPressed = true;
}
if (Input.GetButtonUp("Jump"))
{
isJumpButtonPressed = false;
}
private void CollisionChecks()
{
isGrounded = Physics2D.OverlapBox(groundCheckPos.position, groundCheckSize, 0, groundLayer);
isTouchingWall = Physics2D.OverlapBox(wallCheckPos.position, wallCheckSize, 0, groundLayer);
}
private void PlayerJump()
{
if (isJumpButtonPressed && isGrounded)
{
isGrounded = false;
jumpTimeCounter = jumpTime;
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isJumpButtonPressed = false;
}
if (Input.GetButton("Jump") && !isGrounded)
{
if (jumpTimeCounter > 0)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
else
{
isGrounded = true;
}
}
if (!isJumpButtonPressed)
{
isGrounded = true;
}
}
Answer by spooneystone · Sep 30, 2020 at 01:42 PM
Put your CollisionChecks() in Fixedupdate() as it is checking for the ground.
Answer by EdenAlon · Sep 30, 2020 at 04:18 PM
hmmm.. I tried what you said (spooneystone) and it didn't fix the problem, however, moving all my methods from fixed update to update, solved it. everything is so responsive and the double-jump never occurs.
So I don't get it? how should you use fixed Update? what am I doing wrong? is it really so bad for performance if I put my methods in Update for a 2d platformer?
Answer by sztobar · Oct 10, 2020 at 07:47 PM
@EdenAlon at the end of PlayerJump
method you have a condition:
if (!isJumpButtonPressed)
{
isGrounded = true;
}
so each time a player stops pressing jump button mid-air you reset isGrounded
making it possible to jump again. Can you explain what is intention of this code block?