Currently I have it working where you can jump off the ladder but if you land back on the ladder there are some issues. I’ll work on this bug and try to update with a fix =]
Edit:
Updated the script so that it works, let me know what you think!
ps. you have to “grab the ladder” (press w or d) after a jump which I think is a neat feature and adds flexibility to movement!
second bug found: if you jump into a ladder it acts as kind of an escalator and shoots you upwards. I am currently confused and will pretend this is a speed running feature until I figure out how to fix it
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
[SerializeField] float jumpVelocity = 5f;
[SerializeField] CapsuleCollider2D myCollider;
Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
float gravity;
float gravityMultiplier;
bool isJumping;
private void Start()
{
myAnimator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
gravity = myRigidbody.gravityScale;
}
void Update()
{
Run();
FlipSprite();
ClimbLadder();
}
private void FlipSprite()
{
if (Mathf.Abs(myRigidbody.velocity.x) < Mathf.Epsilon)
{
myAnimator.SetBool("isRunning", false);
return;
}
myAnimator.SetBool("isRunning", true);
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f);
}
private void OnJump(InputValue value)
{
if (!myCollider.IsTouchingLayers(LayerMask.GetMask("Ground")) && !myCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
return;
}
if (value.isPressed)
{
isJumping = true;
myRigidbody.velocity += new Vector2(0f, jumpVelocity);
}
}
private void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
if (myRigidbody.velocity.y < Mathf.Epsilon)
{
isJumping = false;
}
// Debug.Log(moveInput);
}
void ClimbLadder()
{
if (!myCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
myAnimator.SetBool("isClimbing", false);
gravityMultiplier = 1f;
myRigidbody.gravityScale = gravity * gravityMultiplier;
return;
}
if (Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon)
{
myAnimator.SetBool("isClimbing", true);
}
else { myAnimator.SetBool("isClimbing", false); }
gravityMultiplier = 0f;
myRigidbody.gravityScale = gravity * gravityMultiplier;
Vector2 climbVelocity = new Vector2(myRigidbody.velocity.x, moveInput.y * climbSpeed);
if (!isJumping)
{
myRigidbody.velocity = climbVelocity;
}
else
{
myAnimator.SetBool("isClimbing", false);
}
}
}