I need help with implementing ladder climb to my character control script

Hello everyone, I need help with this script,

context: this is a character control script that I made from this tutorial

, I wanted a first person controller so I used another tutorial to make the camera movement that’s a separate script.
It uses the new input.

This script is attached to a character model along with built in character controller, rigid body, capsule and the camera movement script.

The problem is when I reached a stage of implementing the ladder climbing feature I started seeing problems I tried many tutorials to create my ladder feature but all did not work, even chatGPT couldn’t help, I when the assigned key for climb up is pressed character only tries to move, it moves an inch I think gravity is causing the problem but I don’t know how to fix it, I scrapped off the codes that were handling ladder system, I used the tutorials that come first when you search unity ladder climbing on YouTube, include codemonkey his code wasn’t working, I tried using alternative method of ladder climbing like teleporting but it didn’t work out aswell, I am still a beginner.

Here is the script I will appreciate the help

using UnityEngine;
using UnityEngine.UI;

namespace Assets.Script
{
    public class PlayerControls : MonoBehaviour
    {
        [Header("MMOVEMENT PARAMETERS")]
        [SerializeField] private CharacterController controller;
        [SerializeField] private Vector3 playerVelocity;
        [SerializeField, Range(0, 5)] private float jumpHeight = 1.0f;
        [SerializeField] private float gravityValue = -9.81f;
        public Transform player;


        //Animations
        public Animator Anim;
        private float horizontalInput;
        private float verticalInput;

        [Range(-1, -10)] public int fallHeight;



        public PlayerJoystick playerInput;
        public Transform cameraTransform;

        private float speedSmoothVelocity;
        private float currentSpeed;
        private Vector3 currentVelocity;

        //Check velocity
        [Space(10)]
        [Header("VELOCITY CHECK")]
        public Text VelocityText;
        public Rigidbody playerRigidbody;


        [Space(10)]
        [Header("SPRINTING PARAMETERS")]
        //Sprinting
        public Image sprintButton;
        public Color On;
        public Color Off;

        [Space(10)]
        [Header("CROUCH PARAMETERS")]
        public float crouchHeight;
        public float crouchCenter;
        public float OriginalHeight;
        public float OriginalCenter;
        public Image crouchButton;
        [SerializeField] private GameObject infoTextObject;
        [ExecuteAlways, Range(0, 5)] public float infoDuration;

        [Space(10)]
        [Header("LADDER CLIMB PARAMETERS")]
        [SerializeField] private bool climbingLadder;
        [SerializeField] private float LadderSpeed;
        [SerializeField] private GameObject ladderButtons;
        [SerializeField] Button UpArrow;
        [SerializeField] Button DownArrow;
        [SerializeField] GameObject Joystick;
        [SerializeField] Rigidbody rb;
        public CharacterController box;



        [Space(10)]
        [Header("SPEED PARAMETERS")]
        [SerializeField, ExecuteAlways, Range(0, 10)] private float playerSpeed;
        [SerializeField, ExecuteAlways, Range(0, 10)] private float sprintSpeed;
        [SerializeField, ExecuteAlways, Range(0, 10)] private float crouchSpeed;
        [SerializeField, ExecuteAlways, Range(0, 10)] private float walkSpeed;
        private Text speedText;

        [Space(10)]
        [Header("ACTIONS PARAMETERS")]
        [SerializeField] private bool canJump = true;
        [SerializeField] private bool isCrouching;
        [SerializeField] private bool isSprinting;
        [SerializeField] private bool groundedPlayer;

  






        private void Awake()
        {
            playerInput = new PlayerJoystick();
            controller = GetComponent<CharacterController>();
            Anim = GetComponentInChildren<Animator>();
            playerRigidbody = GetComponent<Rigidbody>();
            VelocityText = GameObject.Find("Velocity Text").GetComponent<Text>();
            speedText = GameObject.Find("Speed Text").GetComponent<Text>();
            infoTextObject = GameObject.Find("Info Text");
            sprintButton = GameObject.Find("Sprint").GetComponent<Image>();
            crouchButton = GameObject.Find("Crouch").GetComponent<Image>();
            //ladderButtons = GameObject.Find("Ladder UI");
            //UpArrow = GameObject.Find("Up").GetComponent<Button>();
            //DownArrow = GameObject.Find("Down").GetComponent<Button>();
            //Joystick = GameObject.Find("Joystick");


            Joystick.SetActive(true);
            infoTextObject.SetActive(false);
            ladderButtons.SetActive(false);
          


        }

        private void OnEnable()
        {
            playerInput.Enable();
        }

        private void OnDisable()
        {
            playerInput.Disable();
        }



        void Update()
        {
            groundedPlayer = controller.isGrounded;

            if (groundedPlayer && playerVelocity.y < 0)
            {
                playerVelocity.y = 0f;
            }

            // Get the movement input vector
            Vector2 movementInput = playerInput.PlayerMainMob.Move.ReadValue<Vector2>();
            Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);

            // Transform the move direction to be relative to the camera's rotation
            move = Quaternion.Euler(0, cameraTransform.eulerAngles.y, 0) * move;

            float targetSpeed = movementInput.magnitude * playerSpeed;
            currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, 0.1f);

       
         

            //move the player
            controller.Move(move * currentSpeed * Time.deltaTime);

  

            // Update the animator parameters
            horizontalInput = movementInput.x;
            verticalInput = movementInput.y;

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             
            ///INPUTS///
          

            // Input for Jump
            if (playerInput.PlayerMainMob.Jump.triggered)
            {
                if (canJump)
                {
                    Anim.SetTrigger("Jump");
                    playerVelocity.y += Mathf.Sqrt(jumpHeight * -2f * gravityValue);
                }
                else if (!canJump)
                {
                    infoTextObject.SetActive(true);
                    Invoke("DeActivate", infoDuration);
                }
              
            }

      
            //Check if the player is falling and transition to the appropriate animation blend trees

            if (playerVelocity.y < fallHeight)
            {
                Anim.SetBool("Falling", true);
            }
            else if (playerVelocity.y > -1 && !climbingLadder)
            {
                Anim.SetBool("Falling", false);
            }

            if (!climbingLadder)
            {
            // Apply gravity to the player
            playerVelocity.y += gravityValue * Time.deltaTime;
            controller.Move(playerVelocity * Time.deltaTime);
            }




            //Check Velocity
            Vector3 velocity = playerRigidbody.velocity;
            string velocityString = "Velocity: " + velocity.magnitude.ToString("F2");
            VelocityText.text = velocityString;


            //Check player speed
            speedText.text = "Speed: " + playerSpeed.ToString();



            //Sprinting input
            if (playerInput.PlayerMainMob.Sprint.triggered)
            {

                //set bool
               if  (!isSprinting)
               {
                    isSprinting = true;

                // Play animation
                Anim.SetBool("Sprinting", true);

                // Set the animator parameters to control the sprint animations
                Anim.SetFloat("Horizontal", horizontalInput);
                Anim.SetFloat("Vertical", verticalInput);

                // Button color
                sprintButton.color = On;

                // Increase player speed
                playerSpeed = sprintSpeed;

               }
               else if (isSprinting)
                {
                    //set bool
                    isSprinting = false;

                    // Turn off animation
                    Anim.SetBool("Sprinting", false);

                    // Change color
                    sprintButton.color = Off;

                    if (isCrouching)
                    {
                        playerSpeed = crouchSpeed;
                        // Set the animator to control the crouch animations
                        Anim.SetFloat("Xdirection", horizontalInput);
                        Anim.SetFloat("Ydirection", verticalInput);

                    }
                    else
                    {
                        playerSpeed = walkSpeed;
                        // Set the animator parameters to control the walk animations
                        Anim.SetFloat("x", horizontalInput);
                        Anim.SetFloat("y", verticalInput);
                    }
                  

               }
               //end of sprint check

            }
            else if (playerInput.PlayerMainMob.Sprint.triggered)
            {
                //Cancel crouching
                isCrouching = false;
             
              

                // Turn off animation
                Anim.SetBool("Crouching", false);

                // Change color
                crouchButton.color = Off;

                // Reset the height and center
                controller.height = OriginalHeight;
                controller.center = new Vector3(0, OriginalCenter, 0);
                ///////////////////////////////////////////////

                //Activate crouching

                //set bool
                isSprinting = true;

                // Play animation
                Anim.SetBool("Sprinting", true);

                // Set the animator parameters to control the sprint animations
                Anim.SetFloat("Horizontal", horizontalInput);
                Anim.SetFloat("Vertical", verticalInput);

                // Button color
                sprintButton.color = On;

                // Increase player speed
                playerSpeed = sprintSpeed;



            }

            //Crouch input
            if (playerInput.PlayerMainMob.Crouch.triggered)
            {
                //check  if crouching already
                if (!isCrouching)
                {
                    //set bool
                    isCrouching = true;
                  

                   // Play animation
                    Anim.SetBool("Crouching", true);

                    // Set the animator to control the crouch animations
                    Anim.SetFloat("Xdirection", horizontalInput);
                    Anim.SetFloat("Ydirection", verticalInput);

                    // Change color
                    crouchButton.color = On;

                    // Set the new height and center
                    controller.height = crouchHeight;
                    controller.center = new Vector3(0, crouchCenter, 0);

                    // Change player speed
                    playerSpeed = crouchSpeed;

                  
                }
                else if (isCrouching)
                {
                    //set bool to not crouching
                    isCrouching = false;
                  

                    // Turn off animation
                    Anim.SetBool("Crouching", false);

                    // Change color
                    crouchButton.color = Off;

                    // Reset the height and center
                    controller.height = OriginalHeight;
                    controller.center = new Vector3(0, OriginalCenter, 0);



                    //change speed
                    if (isSprinting)
                    {
                        playerSpeed = sprintSpeed;
                        // Set the animator parameters to control the sprint animations
                        Anim.SetFloat("Horizontal", horizontalInput);
                        Anim.SetFloat("Vertical", verticalInput);
                    }
                    else
                    {
                        playerSpeed = walkSpeed;
                        // Set the animator parameters to control the walk animations
                        Anim.SetFloat("x", horizontalInput);
                        Anim.SetFloat("y", verticalInput);
                    }
                }

            }
            else if (playerInput.PlayerMainMob.Crouch.triggered && isSprinting)
            {
              
             
                    /// cancel sprinting
                 //set bool
                isSprinting = false;
                  

                // Turn off animation
                Anim.SetBool("Sprinting", false);

                // Change color
                sprintButton.color = Off;

                    //Change player speed
                playerSpeed = walkSpeed;
                    /////////

                    //activate crouching

                    //set bool
                    isCrouching = true;
                    
                    // Set the animator to control the crouch animations
                    Anim.SetFloat("Xdirection", horizontalInput);
                    Anim.SetFloat("Ydirection", verticalInput);

                    // Play animation
                    Anim.SetBool("Crouching", true);

                    // Change color
                    crouchButton.color = On;

                    // Set the new height and center
                    controller.height = crouchHeight;
                    controller.center = new Vector3(0, crouchCenter, 0);

                    // Change player speed
                    playerSpeed = crouchSpeed;

                  


              
              
       

            }

            //if player is not sprinting or crouching
            if (!isSprinting && !isCrouching)
            {
                playerSpeed = walkSpeed;
              

                // Set the animator parameters to control the walk animations
                Anim.SetFloat("x", horizontalInput);
                Anim.SetFloat("y", verticalInput);
            }


       
             
            //play animation controls
            if (isSprinting)
            {
                // Turn off animation
                Anim.SetBool("Sprinting", true);
                // Set the animator parameters to control the sprint animations
                Anim.SetFloat("Horizontal", horizontalInput);
                Anim.SetFloat("Vertical", verticalInput);
            }
            if (isCrouching)
            {
                // Play animation
                Anim.SetBool("Crouching", true);

                // Set the animator to control the crouch animations
                Anim.SetFloat("Xdirection", horizontalInput);
                Anim.SetFloat("Ydirection", verticalInput);
            }

            if (isCrouching)
            {
                canJump = false;
            }
            else if (!isCrouching)
            {
                canJump = true;
            }

            //Ladder////


       
        }

 
 


        void DeActivate()
        {
            infoTextObject.SetActive(false);

          
        }


   

     
    }
}

Sounds like it is time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.