Coyote time made infinite jumps

Viewed 24

I added coyote time to a platformer I'm making and I don't know why it made infinite jumps. If somebody fixes it can you please tell me what was broken about it (sorry if this message is sloppy idk how to use this Stack Overflow)

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;

private Rigidbody2D _rigidbody;

private float coyoteTime = 0.2f;
private float coyoteTimeCounter;

[SerializeField] private Rigidbody2D rigidbody2D;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;

private bool isGrounded()
{
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

private void Start()
{
    _rigidbody = GetComponent<Rigidbody2D>();
}

private void Update()
{
    if(isGrounded())
    {
        coyoteTimeCounter = coyoteTime;
    }
    else
    {
        coyoteTimeCounter -= Time.deltaTime;
    }

    var movement = Input.GetAxis("Horizontal");
    transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;

    if(coyoteTimeCounter > 0f && Input.GetButtonDown("Jump"))
    {
        _rigidbody.velocity = new Vector2(_rigidbody.velocity.x, JumpForce);
    }

    if(Input.GetButtonUp("Jump") && _rigidbody.velocity.y > 0f)
    {
        _rigidbody.velocity = new Vector2(_rigidbody.velocity.x, _rigidbody.velocity.y * 0.5f);

        coyoteTimeCounter = 0f;
    }
}

}

1 Answers

I fixed it just had to put the ground check under the player in the unity thingy

Related