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;
}
}