Climb cancelling out Jump

Hey there,

After implementing the Climb functionality I’ve noticed that the Jump has stopped working.

I don’t see this happening for Rick.
I’ve implemented some thing a bit differently (might be relevant, might not be).
The if statement before the jump is commented out so that nothing inhibits the jump functionality while debugging.

Here’s my code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour

{

[SerializeField] float runSpeed = 5f;

[SerializeField] float jumpSpeed = 5f;

[SerializeField] float climbSpeed = 5f;

SpriteRenderer playerSprite = new SpriteRenderer();

Vector2 moveInput;

Rigidbody2D playerRB2D; //- rick did it this way.

Animator characterAnimator = new Animator();

BoxCollider2D playerCapsuleCollider;



void Start()

{

    playerRB2D = GetComponent<Rigidbody2D>();

    characterAnimator = GetComponent<Animator>();

    playerSprite = GetComponent<SpriteRenderer>();

}


void Update()

{

    Climb();

    Run();

    FlipCharacter();

}

void OnMove(InputValue moveButton) //was “clickedButton”

{

    moveInput = moveButton.Get<Vector2>();

}

void OnJump(InputValue jumpButton)

{  

   //if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return;}

    if (jumpButton.isPressed)

    {

        playerRB2D.velocity += new Vector2 (0f, jumpSpeed);

    }

}


void Climb()

{

    Vector2 climbVelocity = new Vector2 (playerRB2D.velocity.x, moveInput.y * climbSpeed);

    playerRB2D.velocity = climbVelocity;

}



void Run()

{

    Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, playerRB2D.velocity.y);

    playerRB2D.velocity = playerVelocity;

   

    if (playerVelocity.x !=0)

    {

        characterAnimator.SetBool ("isRunning", true);

    }

    else

    {

        characterAnimator.SetBool ("isRunning", false);

    }

}



void FlipCharacter()

{

    if (playerRB2D.velocity.x > 0)

    {

        playerSprite.flipX = false;

    }

    if (playerRB2D.velocity.x < 0)

    {

        playerSprite.flipX = true;

    }

}    

}

Your Climb() is completely replacing the y-velocity of the player all the time. What you probably want to do is check if the y-velocity is not 0 and only perform the climb if it is. Something like

void Climb()
{
    if (Mathf.Approximately(moveInput.y, 0f))
    {
        return;
    }
    Vector2 climbVelocity = new Vector2 (playerRB2D.velocity.x, moveInput.y * climbSpeed);
    playerRB2D.velocity = climbVelocity;
}

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

Privacy & Terms