So, it was bugging me when I could not jump from the ladder. The basic problem I found was that the climb method changes the vertical velocity on the next frame when it is called after the jump. That way it stops even though it jumps. Even if jumping to the side, there is not enough movement in one frame to stop touching the ladder. I have implemented a coroutene that sets a jumping boolean for a very small time to true and then turns it back to false. The climbing functions only happen when the jumping boolean is false.
Its not a very elegant solution and I have the feeling that something much more simple will work better. But here is my solution:
private void Climb()
{
bool isClimbing = myCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"));
bool playerHasVerticalVelocity = Mathf.Abs(rb.velocity.y) > Mathf.Epsilon;
if (!isClimbing)
{
rb.gravityScale = initialGravity;
anim.SetBool("isClimbing", false);
return;
}
Vector2 inputVector = new Vector2(rb.velocity.x, Input.GetAxisRaw("Vertical") * climbSpeed);
if (Input.GetButtonDown("Jump"))
{
StartCoroutine(JumpOnLadder());
return;
}
else
{
if(!jumping)
{
rb.velocity = inputVector;
rb.gravityScale = 0;
anim.SetBool("isClimbing", playerHasVerticalVelocity);
}
}
}
IEnumerator JumpOnLadder()
{
rb.gravityScale = initialGravity;
rb.velocity += new Vector2(0f, jumpSpeed);
jumping = true;
yield return new WaitForSeconds(0.1f);
jumping = false;
}
Any criticism or correction welcome. Thank you