The triggering is not working, player is not climbing ladder


my player is not triggering and climbing.

my inspectors





my script

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

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rb;
    Vector2 _moveinput;

    [SerializeField] float _movespeed = 10f;
    [SerializeField] float _jumpspeed = 5f;
    [SerializeField] float _climbspeed = 5f;

    Animator _animator;
    CapsuleCollider2D _capsulecollider;
    bool _isTouchingLadder = false;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        _animator = GetComponent<Animator>();
        _capsulecollider = GetComponent<CapsuleCollider2D>();
    }

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

    void OnMove(InputValue value)
    {
        _moveinput = value.Get<Vector2>();
    }

    void OnJump(InputValue value)
    {
        if (!_capsulecollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }

        if (value.isPressed)
        {
            rb.velocity += new Vector2(0, _jumpspeed);
        }
    }

    void Run()
    {
        Vector2 _playervelocity = new Vector2(_moveinput.x * _movespeed, rb.velocity.y);
        rb.velocity = _playervelocity;

        bool _playerHasHorizontalSpeed = Math.Abs(rb.velocity.x) > Mathf.Epsilon;
        _animator.SetBool("isRunning", _playerHasHorizontalSpeed);
    }

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

    void ClimbLadder()
    {
        if (!_isTouchingLadder)
        {
            // Turn off climbing animation if not touching ladder
            _animator.SetBool("isClimbing", false);
            return;
        }

        Vector2 _climbvelocity = new Vector2(rb.velocity.x, _moveinput.y * _climbspeed);
        rb.velocity = _climbvelocity;

        bool _playerIsClimbing = Math.Abs(rb.velocity.y) > Mathf.Epsilon;
        _animator.SetBool("isClimbing", _playerIsClimbing);
    }

    // Trigger detection for ladder
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("Climbing"))
        {
            _isTouchingLadder = true;
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("Climbing"))
        {
            _isTouchingLadder = false;
        }
    }
}

At a first glance, your code looks like it should work.

Can you check that you painted the ladder tiles onto the ladder layer rather than the ground layer? That used to trip me up a lot.

Also it’s probably a good idea to set the gravity scale to zero while on the ladder and set it back to the start gravity when not on the ladder, since forces can still take effect even after your scripts have executed.

myRigidbody.gravityScale = 0f;

Privacy & Terms