I’ve implemented the ladder system a slightly different way because I noticed that previously the Player couldn’t jump when they were touching the ground and ladder layers at the same time. Everything works except for when the player goes up the ladder from the ground, the climbing animation doesn’t play. This has something to do with my NotClimbingLadder Method with the second if statement. Any ideas on how to fix this issue?
Video of problem: https://youtu.be/lVgt25ZQQEM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D rb2D;
Animator animator;
CapsuleCollider2D capsuleCollider2D;
LadderLogic ladderLogicScript;
float gravityStartScale;
[SerializeField] float moveSpeed = 5f;
[SerializeField] float jumpHeight = 5f;
[SerializeField] float climbSpeed = 5f;
[SerializeField] float climbSpeedDOWN = -3f;
void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
capsuleCollider2D = GetComponent<CapsuleCollider2D>();
ladderLogicScript = FindObjectOfType<LadderLogic>();
gravityStartScale = rb2D.gravityScale;
}
void Update()
{
Run();
FlipSprite();
NotClimbingLadder();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void OnJump(InputValue value)
{
if(!capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
return;
}
if(capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
if (value.isPressed)
{
rb2D.velocity += new Vector2 (0f, jumpHeight);
}
}
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * moveSpeed, rb2D.velocity.y);
rb2D.velocity = playerVelocity;
if (Mathf.Abs(rb2D.velocity.x) > Mathf.Epsilon)
{
animator.SetBool("IsRunning", true);
}
else
{
animator.SetBool("IsRunning", false);
}
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb2D.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2 (Mathf.Sign(rb2D.velocity.x), 1f);
}
}
void NotClimbingLadder()
{
if(!capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
{
rb2D.gravityScale = gravityStartScale;
animator.SetBool("IsClimbing", false);
return;
}
if(capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
animator.SetBool("IsClimbing", false);
}
}
void OnClimbingUP(InputValue value)
{
if(capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
{
Vector2 climbVelocity = new Vector2(rb2D.velocity.x, moveInput.y * climbSpeed);
rb2D.velocity = climbVelocity;
rb2D.gravityScale = 0f;
bool playerHasVerticalSpeed = Mathf.Abs(rb2D.velocity.y) > Mathf.Epsilon;
animator.SetBool("IsClimbing", playerHasVerticalSpeed);
Debug.Log("ClimbingUP");
}
}
void OnClimbingDOWN(InputValue value)
{
if(capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
{
Vector2 climbVelocityDOWN = new Vector2(rb2D.velocity.x, moveInput.y * climbSpeedDOWN * -1);
rb2D.velocity = climbVelocityDOWN;
rb2D.gravityScale = 0f;
bool playerHasVerticalSpeed = Mathf.Abs(rb2D.velocity.y) > Mathf.Epsilon;
animator.SetBool("IsClimbing", playerHasVerticalSpeed);
}
}
}