Player can't jump when moving left/right and jump higher in random moments

Hi, today when i was testing levels in game i saw that my character can’t sometimes jump when moving.
Second problem is that my character sometimes jump higher than he should (it usually happens when you are on edge of platform), any fixes?

Player code:

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

public class Player : MonoBehaviour
{

    private Rigidbody2D myRigidbody2D;

    [SerializeField]
    private float moveSpeed;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;
    private bool isGrounded;
    private bool jump;

    [SerializeField]
    private float jumpForce;



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

    // Update is called once per frame

    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        HandleMovement(horizontal);
        isGrounded = IsGrounded();
        ResetValues();
    }

    private void HandleMovement(float horizontal)
    {

        myRigidbody2D.velocity = new Vector2(horizontal * moveSpeed, myRigidbody2D.velocity.y);

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            jump = true;
        }
        if (isGrounded && jump)
        {
            isGrounded = false;
            myRigidbody2D.AddForce(new Vector2(0, jumpForce));
            isGrounded = false;
        }
    }
    private void ResetValues()
    {
        jump = false;
    }

    private bool IsGrounded()
    {
        if (myRigidbody2D.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }


}

Code for better jumping:

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

public class BetterJump : MonoBehaviour
{
    public float fallMultiplier = 2.5f;
    public float lowMultiplier = 2.5f;

    Rigidbody2D rb;

    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (rb.velocity.y < 0)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }
    }
}

Input should be checked in the update

float horizontal;
void Update() {
horizontal = Input.GetAxis("Horizontal");
if (Input.GetKeyDown(KeyCode.UpArrow)) {
jump = true;
}
}
1 Like