When ClimbLadder is activated, player doesnt jump

So i have problem with jumping. When ClimbLadder() function is not in Update so it isnt turn on player is normally jumping. But when ClimbLadder() function is in update player can not jump. Only when i press some vertical key and symontenously pressing space. Sometimes will player jump. I not really know whats happening. The layers are set correctly and same with collisions. Can someone tell me whats the problem ?

Video: https://www.youtube.com/watch?v=iZNQtamKvKE

Player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour
{

// Config
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;

// State
bool isAlive = true;

// Cashed component references
Rigidbody2D myRigidBody;
Animator myAnimator;
Collider2D myCollider2D;

//Message then methods

// Start is called before the first frame update
void Start()
{
    myRigidBody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myCollider2D = GetComponent<Collider2D>();
}

// Update is called once per frame
void Update()
{
    Run();
    FlipSprite();
    Jump();
    ClimbLadder();

}

private void Run()
{
    float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // -1 to +1
    Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
    myRigidBody.velocity = playerVelocity;

    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
}

private void Jump()
{
    if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }

    if (CrossPlatformInputManager.GetButtonDown("Jump"))
    {
        Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
        myRigidBody.velocity += jumpVelocityToAdd;
    }
}

private void ClimbLadder()
{
    if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }

    float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
    Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
    myRigidBody.velocity = climbVelocity;

}

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

}

}

Hi Jindra,

Actually, the player can jump while climbing the ladder. See the order of the method calls in the Update method. However, in ClimbLadder, we override the velocity value. Since our jump depends on the velocity, you cannot see any jumping. What you could try is to swap the last two method calls in Update. Maybe that’ll fix the issue.


See also:

So update. I swap the jump and ladder climbing function. But i feels like the problem in this line. Because when i press w or up the climbing animation can start even when i am not on ladder, so this if statement isnt working. But why is that I really dont know. Layer has same name so when could be the problem ?

if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }

You are right. I completely forgot the conditions, which are also relevant here. return terminates the method immediately.

Check the condition in the Jump method and pay attention to the !, which means “not”. When we are on the “Climbing” layer, we are cannot be on the “Ground” layer and vice versa. Rick used this fact to wire up his logic.

If you want a different behaviour in your game, you’ll have to write your own solution. The things you need are already there. You just have to use them in a different way so they match your idea.

If you don’t know where to start, ask yourself the question: WHEN is something supposed to happen? The answer is your if-condition. Log a message into your console. When the message pops up when something is supposed to happen, you completed the first step.

The next step would be to make the “something” happen.

Finnally I found the problem :smiley: . When i was doing Rick challenge i copied foreground layer and change it to Climbing and I add ladders. But I forgot on removing tiles from foreground XD. So when it started the function with jump and ladders start to fought among themselves. My silly mistake.

1 Like

I’m glad the solution was so simple. :slight_smile:

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

Privacy & Terms