Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Issue with Ladder Climbing script.

Discussion in '2D' started by Peter-Hepden, Feb 12, 2023.

  1. Peter-Hepden

    Peter-Hepden

    Joined:
    Feb 4, 2023
    Posts:
    2
    So basically my issue is when I use this ladder script I made it brakes my player jumping mechanism. When the ladder script is active my player pretty much can't get off the ground. I am still new to Games Dev and unity so I am sorry if this is something stupid!

    Ladder Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LadderMovement : MonoBehaviour
    6. {
    7.     private Rigidbody2D playerRb;
    8.     private Animator animator;
    9.     private bool isClimbing;
    10.     public float climbSpeed = 5.0f;
    11.  
    12.     void Start()
    13.     {
    14.         playerRb = GetComponent<Rigidbody2D>();
    15.         animator = GetComponent<Animator>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (isClimbing)
    21.         {
    22.             playerRb.velocity = new Vector2(0, Input.GetAxisRaw("Vertical") * climbSpeed);
    23.             animator.SetBool("isClimbing", true);
    24.         }
    25.         else
    26.         {
    27.             playerRb.velocity = new Vector2(playerRb.velocity.x, 0);
    28.             animator.SetBool("isClimbing", false);
    29.      
    30.         }
    31.     }
    32.  
    33.     void OnTriggerStay2D(Collider2D other)
    34.     {
    35.         if (other.tag == "Ladder")
    36.         {
    37.             isClimbing = true;
    38.         }
    39.     }
    40.  
    41.     void OnTriggerExit2D(Collider2D other)
    42.     {
    43.         if (other.tag == "Ladder")
    44.         {
    45.             isClimbing = false;
    46.          
    47.         }
    48.     }
    49. }
    50.  
    PlayerMovement Script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public Rigidbody2D playerRb;
    6.     public float speed;
    7.     public float input;
    8.     public SpriteRenderer spriteRenderer;
    9.     public float jumpForce;
    10.  
    11.     public LayerMask groundLayer;
    12.     private bool isGrounded;
    13.     public Transform feetPosition;
    14.     public float groundCheckCircle;
    15.     public Animator playerAnim;
    16.  
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         input = Input.GetAxisRaw("Horizontal");
    22.  
    23.         if (input < 0)
    24.         {
    25.             spriteRenderer.flipX = true;
    26.         }
    27.         else if (input > 0)
    28.         {
    29.             spriteRenderer.flipX = false;
    30.         }
    31.  
    32.         isGrounded = Physics2D.OverlapCircle(feetPosition.position, groundCheckCircle, groundLayer);
    33.  
    34.         if (isGrounded == true && Input.GetButtonDown("Jump"))
    35.         {
    36.             playerRb.velocity = Vector2.up * jumpForce;
    37.         }
    38.  
    39.      
    40.     }
    41.  
    42.     private void FixedUpdate()
    43.     {
    44.         playerRb.velocity = new Vector2(input * speed, playerRb.velocity.y);
    45.      
    46.     }
    47.  
    48.  
    49. }
     
    Last edited: Feb 12, 2023
  2. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    169
    Just had a quick look over it, it's probably due to the fact you've got conflicting logic with the Rb from the player's velocity. In the PlayerMovement Script you're saying, make the velocity one thing, then when it's triggered in the OnTriggerStay2D in your Ladder script, it's overwriting the Rb velocity again. Your best bet though is to stick in a few Debug.Log(playerRb.velocity) in various parts of the code and see what's happening.
     
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Line 27 of the ladder script looks problematic to me. You're setting the y velocity of the rigidbody2d to zero every frame which will break your jumping. Try setting it to
    playerRb.velocity.y
    instead.

    Edit: Actually, just delete that line completely. After reflecting for a few minutes, I realized that with the above change, the line doesn't do anything at all haha.
     
    Last edited: Feb 13, 2023
    Peter-Hepden likes this.
  4. Peter-Hepden

    Peter-Hepden

    Joined:
    Feb 4, 2023
    Posts:
    2
    Thank you! That worked!
     
    Unrighteouss likes this.