Player is NOT jumping as it should

It can move, it can do everything but it can jump. It’s read on the console that IS jumping but the character is not jumping.

CapsuleCollider2D myCapsuleCollider;
BoxCollider2D myBoxCollider;

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

    if (value.isPressed)
    {
        myRb2d.velocity += new Vector2(0f, jumpSpeed);
    }
}

void ClimbLadder()
{
    if (!myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"))) 
    {
        myRb2d.gravityScale = gravityScaleAtStart;
        myAnim.SetBool("isClimbing", false);
        return; 
    }

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

    bool playerHasVerticalSpeed = Mathf.Abs(myRb2d.velocity.y) > Mathf.Epsilon;
    myAnim.SetBool("isClimbing", playerHasVerticalSpeed);

}

Hi,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Does the OnJump method get called if you click the “jump” key?

That’s exactly what it shows in the console… it reads that it is jumping but the character is not jumping in the game tab

I’m afraid I cannot see any Debug.Log in the code you shared.

Ah, there’s a screenshot that displays the up arrow reading

Here, should you need the code

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

public class PlayerMovement : MonoBehaviour
{
    Vector2 moveInput;
    Rigidbody2D myRb2d;
    Animator myAnim;
    CapsuleCollider2D myCapsuleCollider;
    
    /* Remember to come back to this! If need be, check 92. Prevent Wall Jump video to
    be able to figure out how to not jump more than once of a wall. Commented lines are 
    the code that Rick used on his end. */
    // BoxCollider2D myBoxCollider;

    [SerializeField] float runSpeed = 2.5f;
    [SerializeField] float jumpSpeed = 3f;
    [SerializeField] float climbSpeed = 3f;
    float gravityScaleAtStart;

    // Start is called before the first frame update
    void Start()
    {
        myRb2d = GetComponent<Rigidbody2D>();
        myAnim = GetComponent<Animator>();
        myCapsuleCollider = GetComponent<CapsuleCollider2D>();
       //myBoxCollider = GetComponent<BoxCollider2D>();
        gravityScaleAtStart = myRb2d.gravityScale;
    }

    // Update is called once per frame
    void Update()
    {
        Run();
        FlipPlayer();
        ClimbLadder();
    }

    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
        Debug.Log(moveInput);
    }
    void OnJump(InputValue value)
    {
        if(!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }

        if (value.isPressed)
        {
            myRb2d.velocity += new Vector2(0f, jumpSpeed);
        }
    }
    void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRb2d.velocity.y);
        myRb2d.velocity = playerVelocity;

        //Flip the state to running
        bool playerHasHorizontalSpeed = Mathf.Abs(myRb2d.velocity.x) > Mathf.Epsilon;
        
        myAnim.SetBool("isRunning", playerHasHorizontalSpeed);
    }

    void FlipPlayer()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(myRb2d.velocity.x) > Mathf.Epsilon;

        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2(Mathf.Sign(myRb2d.velocity.x), 1f);
        }
    }

    void ClimbLadder()
    {
        if (!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"))) 
        {
            myRb2d.gravityScale = gravityScaleAtStart;
            myAnim.SetBool("isClimbing", false);
            return; 
        }

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

        bool playerHasVerticalSpeed = Mathf.Abs(myRb2d.velocity.y) > Mathf.Epsilon;
        myAnim.SetBool("isClimbing", playerHasVerticalSpeed);

    }
}

@Nina Mind you that I went back to what it was before so I continue with other lectures that thankfully I could just continue without worrying too much about the wall jump issue

The wall jump problem gets solved in one of the videos.

Regarding the general jump, I can still not see any Debug.Log in the OnJump method. OnMove is not the jump method. In the OnJump method, there is an if-statement, and in it’s code block, we terminate this method immediately. Knowing that OnMove gets called is nice and all but if the OnJump method gets terminated (or not called at all), the second if-block will never get executed. The second if-block makes the player jump. User input values in the OnMove method do not make the player jump. I hope the idea behind placing Debug.Logs strategically to debug code is clear now.

Privacy & Terms