I thought that it was weird for the main player to look like he is standing on the ladder. So I made a new animation with one of the climbing sprites. I also made a new animator state that I named “ClimbIdling” and made a new bool named “isClimbIdling” in the animator.
Here’s my climbLadder code:
private void ClimbLadder()
{
if (!_collider2D.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
ResetClimbingState();
return;
}
float controlThrow = CrossPlatformInputManager.GetAxis("Vertical"); // value is between -1 to +1.
Vector2 climbVelocity = new Vector2(_rigidbody2D.velocity.x, controlThrow * _climbSpeed);
_rigidbody2D.velocity = climbVelocity;
SetClimbingState(controlThrow);
}
private void ResetClimbingState()
{
_rigidbody2D.gravityScale = _initialGravityScale;
_animator.SetBool("isClimbing", false);
_animator.SetBool("isClimbIdling", false);
}
private void SetClimbingState(float controlThrow)
{
if (!_collider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
_animator.SetBool("isClimbing", true);
if (controlThrow == 0)
{
_rigidbody2D.gravityScale = 0;
_animator.SetBool("isClimbIdling", true);
}
else
{
_rigidbody2D.gravityScale = _initialGravityScale;
_animator.SetBool("isClimbIdling", false);
}
}
else
{
_animator.SetBool("isClimbing", false);
}
}