Bug with the ladder

Sometimes my Player does not want to touch the Ladder. It happens not often, but sometimes he even falls from it.


Input works, but player does not touch the ladder.

Collision detection for player is:
Screenshot_8

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    Vector2 moveInput;
    Rigidbody2D myRigidbody;
    Animator myAnimator;
    CapsuleCollider2D myCollider;

    [SerializeField] float runSpeed = 5f;
    [SerializeField] float jumpStrength = 10f;
    [SerializeField] float climbSpeed = 3f; 
    float gravityScaleAtStart;

    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        myCollider = GetComponent<CapsuleCollider2D>();
        gravityScaleAtStart = myRigidbody.gravityScale;

    }

    void Update()
    {
        Run();
        FlipSprite();
        ClimbLadder();
    }

    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
        Debug.Log(moveInput);
    }

    void OnJump(InputValue value)
    {
        if(!myCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
        if(value.isPressed)
        {
            myRigidbody.velocity += new Vector2 (0f, jumpStrength);
        }
        
    }

    void ClimbLadder()
    {   
        if(!myCollider.IsTouchingLayers(LayerMask.GetMask("Ladder"))) 
        {
            myRigidbody.gravityScale = gravityScaleAtStart;
            myAnimator.SetBool("isClimbing", false);
            Debug.Log("NoT on the ladder");
            return; 
            
        }

        Vector2 climbVelocity = new Vector2 (myRigidbody.velocity.x, moveInput.y * climbSpeed);
        myRigidbody.velocity = climbVelocity;
        myRigidbody.gravityScale = 0f;

        bool playerIsClimbing = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
        myAnimator.SetBool("isClimbing", playerIsClimbing);
        Debug.Log("On the ladder");
    }

    void Run()
    {
        Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;
        
        bool platerHasHorisontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("isRunning", platerHasHorisontalSpeed);
                
    }

    private void FlipSprite()
    {
        bool platerHasHorisontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
        if(platerHasHorisontalSpeed){
            transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x), 1f);
        }
        
    }
}

Hi,

First of all, check the ladder collider. And check on which layer mask the ladder is. Double-check the spelling of “Ladder” both in your code and Unity.

Did this help you fix the issue?


See also:

Double checked everything, overall it works as you can see below.
Screenshot_9
Screenshot_10
But occasionally Player can fall down, and it starts to work again after goin left-right away from the ladder.

Screenshot_11


Thank you for the screenshots. It’s a bit difficult to see but it looks as if the ladder collider was a bit too wide. If the player happens to be between the two edges, he will fall down. Our algorithm uses the edge of the ladder collider. Edit the Physics Shape and make the ladder collider narrower. Then save the shape and test your game again.

2 Likes

Totally worked, that was the issue.
Ladder collider was bit wider. Thank you!
Screenshot_14

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms