When I jump I can't move left and right

when the player is jumping it can’t move left and right it can when it is falling.
this is the code:

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;

    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private float groundRayCastLength;
    private bool onGround;

    [SerializeField] private float acceleration;
    [SerializeField] private float linearDrag;
    [SerializeField] private float maxSpeed = 15;

    [SerializeField] private float jumpForce = 12f;
    [SerializeField] private float airLinearDrag = 2.5f;

    private float jumpTimeCounter;
    public float jumpTime;
    private bool isJumping;
    private bool canJump => Input.GetButtonDown("Jump") && onGround;
    private bool changingDirection => (rb.velocity.x > 0f && movement < 0f) || (rb.velocity.x < 0f && movement > 0f);
    private float movement;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (canJump)
        {
            Jump();
        }
       
    }

    
    private void checkCollisions()
    {
        onGround = Physics2D.Raycast(transform.position, Vector2.down, groundRayCastLength, groundLayer);
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, transform.position + Vector3.down * groundRayCastLength);
    }
    private void Jump()
    {
        isJumping = true;
        jumpTimeCounter = jumpTime;
        
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    }
    private void MoveCharacter()
    {
        rb.AddForce(new Vector2(movement,0f) * acceleration);

        if(Mathf.Abs(rb.velocity.x) > maxSpeed)
        {
            rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x)* maxSpeed,rb.velocity.y);
          
        }
    }
    private void ApplyLinearDrag()
    {
        if (Mathf.Abs(movement)< 0.4f || changingDirection)
        {
            rb.drag = linearDrag;
        }
        else
        {
            rb.drag = 0f;
        }
    }
    private void ApplyAirLinearDrag()
    {
        rb.drag = airLinearDrag;
    }
    private void FixedUpdate()
    {
        //moving left and right
        movement = Input.GetAxis("Horizontal");

        if (Input.GetKey(KeyCode.Space) && isJumping)
        {
            if (jumpTimeCounter > 0)
            {
                rb.velocity = Vector2.up * jumpForce;
                jumpTimeCounter -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }

        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumping = false;
        }

        MoveCharacter();
        checkCollisions();

        //flipping the player
        if (movement > 0)
        {
            transform.localScale = new Vector2(1, 1);
        }
        if (movement < 0)
        {
            transform.localScale = new Vector2(-1, 1);
        }

        

        if (onGround)
        {
            ApplyLinearDrag();
        }
        else
        {
            ApplyAirLinearDrag();
      
        }
    }
}

Okay, thanks. I might see the problem now. It looks like in your if (jumpTimeCounter > 0) block, you make rb.velocity equal the vertical jump force, without preserving its velocity on the X axis. I think you should replace

rb.velocity = Vector2.up * jumpForce;

with

rb.velocity.y = jumpForce;

or

rb.velocity = new Vector2(rb.velocity.x, jumpForce);

and let me know if there’s still a problem.

You need to keep the current velocity from before you start jumping the same when you do start jumping. I had the same problem not that long ago.

Hi KolibrieGames, I’m not sure why your character can’t be controlled in the air. I have some guesses, but I’ll need to ask some questions before I have a chance to help…

  1. Does your character jump in an arc, keeping the speed it had when on the ground, or does it stop dead right as you lift off the ground?

  2. Does your character ever reach max speed? If you’re not sure, try printing to the console in the if(Mathf.Abs(rb.velocity.x) > maxSpeed) block to see if it does.

  3. What are the values of the jumpTime, acceleration and linearDrag variables? I see that they’re declared, but I never see them being assigned to.

One of my guesses is that the speed never goes above airLinearDrag’s value, and therefore your movement is stopped by the drag before it can even get started.

  1. The character stops dead right as it lifts the ground.
  2. The character reaches max speed.
  3. JumpTime = 0.35
    acceleration = 75
    linear drag = 25
    I tried removing airLinearDrag to test if that was the problem but it didn’t fix it.