Tile_Vainia jump ladder

So I’m doing Tile_Vainia & I can’t figure out how to get the player to jump off of ladders. I’ve tried a few solutions from udemy & the gamedev community site (this site) & I can’t figure it out.

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

public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
[SerializeField] Vector2 deathKick = new Vector2(10f, 10f);
[SerializeField] GameObject bullet;
[SerializeField] Transform gun;

Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;
BoxCollider2D myFeetCollider;
float gravityScaleAtStart;

bool isAlive = true;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myBodyCollider = GetComponent<CapsuleCollider2D>();
    myFeetCollider = GetComponent<BoxCollider2D>();
    gravityScaleAtStart = myRigidbody.gravityScale;
}

void Update()
{
    if (!isAlive) { return; }
    Run();
    FlipSprite();
    ClimbLadder();
    Die();
}

void OnFire(InputValue value)
{
    if (!isAlive) { return; }
    Instantiate(bullet, gun.position, transform.rotation);
}

void OnMove(InputValue value)
{
    if (!isAlive) { return; }
    moveInput = value.Get<Vector2>();
}

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

    if (value.isPressed)
    {
        // do stuff
        myRigidbody.velocity += new Vector2(0f, jumpSpeed);
    }
}

void Run()
{
    Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;

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

}

void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

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

void ClimbLadder()
{
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
    {
        myRigidbody.gravityScale = gravityScaleAtStart;
        myAnimator.SetBool("isClimbing", false);
        return;
    }

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

    bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
    myAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
}

void Die()
{
    if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))
    {
        isAlive = false;
        myAnimator.SetTrigger("Dying");
        myRigidbody.velocity = deathKick;
        FindObjectOfType<GameSession>().ProcessPlayerDeath();
    }
}

}

Hi David,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? For instance, it might be interesting to figure out when the OnJump method gets executed exactly because of these lines in the Run and the ClimbLadder methods:

myRigidbody.velocity = playerVelocity;
myRigidbody.velocity = climbVelocity;

Since the jumping/running/climbing is just “velocity”, try to figure out if the velocity values are the expected values during runtime.

I’m doing Debug.Log. Getting nothing in the console. What boggles me is why doesn’t simply adding a layer mask for “Climbing” to jump make it possible. Well I’ll try to figure it out. I’m not sure what you mean but I’ll keep trying different things until something works.

The issue that’s confusing me with the playerHasVerticleSpeed as a way to jump is what if the player has 0 vertical speed & it just touching a “Climbing” but still wants to jump so he/she can dodge an enemy or just want to jump.

Are the messages enabled in the console? If nothing appears even though the messages are enabled, that could explain why the player is not able to jump off ladders. Maybe the layers are not assigned to the ladder and the ground.

I don’t know what the player does exactly but in the following line of code, the x-value gets overridden by a value that does not depend on the velocity: Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);.

Same here for the y-value: Vector2 climbVelocity = new Vector2(myRigidbody.velocity.x, moveInput.y * climbSpeed);.

Well I have great news & bad news. I never was able to figure this out. What happened is I found a code on the community which gives me something better then what I actually wanted. The person’s code is genius. I had to re-write it 3x but so what I finally got it to work.

If anyone else has this problem follow this link:

Thanks for sharing the solution. :slight_smile:

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

Privacy & Terms