How does the player jump while on the ladder?
hi everyone. I prepared a simple scene for my game that I plan to do. So it's not quite finished yet. but i got stuck on one thing. The character does not jump while on the ladder. I think the problem is caused by gravityscale. Gravity value is 0 when I'm on the ladder. I couldn't do it no matter what I tried. I hope you know a way :) thanks.
here is code:
using UnityEngine; using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour { [Header("Player Stats")] [SerializeField] private float runSpeed;
[SerializeField] private float climbSpeed;
[SerializeField] private float jumpAmount;
public bool isLadder;
public bool isClimbing;
[Header("Player Specifications")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
private float horizontal;
private float vertical;
private Rigidbody2D rb;
private BoxCollider2D playerCollider;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
rb.velocity = new Vector2(horizontal * runSpeed, rb.velocity.y);
FlipPlayer();
if (isLadder && Mathf.Abs(vertical) > 0f)
{
isClimbing = true;
}
}
private void FixedUpdate()
{
if (isClimbing)
{
rb.gravityScale = 0f;
rb.velocity = new Vector2(rb.velocity.x, vertical * climbSpeed);
}
else
{
rb.gravityScale = 10f;
}
}
public void Move(InputAction.CallbackContext context)
{
horizontal = context.ReadValue<Vector2>().x;
vertical = context.ReadValue<Vector2>().y;
}
public void Jump(InputAction.CallbackContext context)
{
if (context.performed && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpAmount);
}
if (context.canceled && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void FlipPlayer()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
isLadder = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
isLadder = false;
isClimbing = false;
}
}
}
Your answer

Follow this Question
Related Questions
Problems with jumping in Doodle Jump like game 2 Answers
My jump scripit wont work. i have been tryingtt o make it work for an eternity. please help. 1 Answer
How to stop my character from infinite jumping? 2 Answers
[Solved, kind of] Assign Multiple Keys for jumping? 0 Answers
Jumping only once (2D) 1 Answer