Why can't I jump from ladders?

Hello,
I’m trying to figure out why I cant jump from ladders

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


public class Player : MonoBehaviour
{   
    //Parametrs for config
    [SerializeField] float runspeed = 5f;
    [SerializeField] float jumpSpeed = 5f;
    [SerializeField] float climbSpeed = 5f;

    //states
    bool isAlive = true;

    //Cached comp ref
    Rigidbody2D myrigidbody;
    Animator myAnimator;
    Collider2D myCollider2D;
    float gravityScaleAtStart;


    // Start is called before the first frame update
    void Start()
    {
        myrigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        myCollider2D = GetComponent<Collider2D>();
        gravityScaleAtStart = myrigidbody.gravityScale;
    }

    // Update is called once per frame
    void Update()
    {
        Run();
        Flipsprite();
        Jump();
        ClimbLadder();
        

    }

    private void Run()
    {
        float controlThrow = Input.GetAxis("Horizontal");
        Vector2 playerVelocity = new Vector2(controlThrow * runspeed ,myrigidbody.velocity.y);
        myrigidbody.velocity = playerVelocity;

        bool playerhorizontalSpeed = Mathf.Abs(myrigidbody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Running", playerhorizontalSpeed);
    }

        private void Jump()
    {
        if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground"))){return;}

        if (Input.GetButtonDown("Jump"))
        {
            Vector2 jumpVelocityAdd = new Vector2(0f, jumpSpeed);
            myrigidbody.velocity += jumpVelocityAdd;
        }
        
    }
    private void ClimbLadder()
    {
        if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) 
        {
            myAnimator.SetBool("Climbing",false);
            myrigidbody.gravityScale =gravityScaleAtStart;
            return;
        }

         float controlThrow = Input.GetAxis("Vertical");
         Vector2 climbVelocity= new Vector2(myrigidbody.velocity.x, controlThrow * climbSpeed);
         myrigidbody.velocity = climbVelocity;
         myrigidbody.gravityScale = 0f;

         bool playerHasVertivalSpeed = Mathf.Abs(myrigidbody.velocity.y) > Mathf.Epsilon;
         myAnimator.SetBool("Climbing",playerHasVertivalSpeed);
    }
    
    private void Flipsprite()
    {
    bool playerhorizontalSpeed = Mathf.Abs(myrigidbody.velocity.x) > Mathf.Epsilon;
    
    if (playerhorizontalSpeed)
    {
         transform.localScale = new Vector2 (Mathf.Sign(myrigidbody.velocity.x), 1f);   
    }
    }
}


everything is working great…but this issue is bugging me for two hours.
I believe that is have something to do with:
if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask(“Ground”))){return;}
because the ladder is on “Climbing mask”

1 Like

Hi! Welcome to the community!

You are right, that’s the reason why you can’t jump on ladders, if you want to jump when on a ladder you’ll have to find a way to check for both layers, which I should warn you, it might cause some unexpected behaviors down the road. I suggest you finish the section before implementing this or something else just to avoid conflicts between your features and the course’s mechanics, specially if you are somewhat new to coding.

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

Privacy & Terms