Problem with jump in Unity

I don’t understand why I press space many times for the character to jump, but sometimes it works just once.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
public class Movement : MonoBehaviour
{
    [SerializeField]
    private float moveForce = 10f;
    [SerializeField]
    private float jumpForce = 11f;
    float movementX;
    [SerializeField]
    private Animator anim;
    private Rigidbody2D mybody;
    private SpriteRenderer sr;
    // Start is called before the first frame update
private void Awake(){
    mybody= GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    sr= GetComponent<SpriteRenderer>();
}
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        AnimationPlayer();
        PlayerMoveKeyBoard();
    }
    private void FixedUpdate(){
           PlayerJump();
    }
    void PlayerMoveKeyBoard(){
         movementX = Input.GetAxisRaw("Horizontal");
        transform.position += new Vector3(movementX, 0f, 0f) * Time.deltaTime * moveForce;
    }
    void AnimationPlayer(){
        // move to right
        if(movementX >0){
        anim.SetBool("Walk", true);
            sr.flipX= false;
        }
        //move to left
        else if(movementX < 0){
            anim.SetBool("Walk", true);
            sr.flipX= true;
      
        }
        // idle
        else{
            anim.SetBool("Walk", false);
        }
    }
    void PlayerJump(){
        if(Input.GetButtonDown("Jump")){
            mybody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
            Debug.Log("Space press");
        }
    }

}

Input.GetKeyDown is only valid during Update. I think you can get lucky sometimes if FixedUpdate and Update happen at the same time, but in general you should not use GetKeyDown in FixedUpdate.

Likewise, physics is processed in sync with FixedUpdate, so any modifications to the RigidBody should be done during FixedUpdate.

I have usually relied on code like this:

bool jumpRequest = false;
void Update()
{
    //This will also typically check whether the character is on the ground,
    //but I'm omitting that detail for this example.
    if(Input.GetKeyDown(KeyCode.Space))
        jumpRequest = true;
}

void FixedUpdate()
{
    if(jumpRequest)
    {
        //<Apply force or velocity or however you do jump>

        jumpRequest = false;
    }
}

Hi,

After testing your code, it seems like FixedUpdate() method is causing the issue. I tested it in the Update() method and it seems to work fine.

    void Update()
    {
        AnimationPlayer();
        PlayerMoveKeyBoard();
        PlayerJump();
    }

Also here are some links to better understand why this happens:

And an old thread of similar issues to the one you described:
https://forum.unity.com/threads/fixed-update-not-consistently-executing-code-posted.828480/

I hope this helps.