Jumping stops x movement

Ok so im very new to coding, and i’ve been trying to make a movement script for my 2D player, everything is going good except for the jumping part of the script , so in the jumping part when i Jump the movement along the x Axis stops moving, so whilst im running and Jump he just stops and jumps no movement on the X axis whilst in air

This is my Script

using UnityEngine;

public class Player_Movement : MonoBehaviour
{
    public int Speed;
    private Animator PlayerAnimation;
    public bool facingRight = false;
    private float MoveX;
    public bool sprintToggle = false;
    public float JumpForce;
    public bool IsGrounded;



    void Start()
    {
        facingRight = false;
        sprintToggle = false;
        PlayerAnimation = GetComponent<Animator>();
        Speed = 13;
        JumpForce = 1250f;
    }



    private void OnCollisionEnter2D(Collision2D collision) //DETECTS IF PLAYER IS ON GROUND
    {
        if(collision.gameObject.tag == "Ground")
        {
            IsGrounded = true;
        }
    }
    private void OnCollisionExit2D(Collision2D collision) // DETECTS IF PLAYER IS OFF OF GROUND
    {
        if(collision.gameObject.tag == "Ground")
        {
            IsGrounded = false;
        }
    }


    void Update()
    {
        PlayerMove();
        Moving();
        SprintToggle();
        Jump();
    }


    public void PlayerMove() //MOVES HORIZONTALLY AND FLIPS HORIZONTALLY
    {
        if (IsGrounded == true)
        {
            MoveX = Input.GetAxis("Horizontal");
            {
                var x = MoveX * Time.deltaTime * Speed;
                {

                    transform.Translate(x, 0, 0);

                }
            }
            if (MoveX < 0.0f && facingRight == true)
            {
                FlipPlayer();

            }
            if (MoveX > 0.0f && facingRight == false)
            {
                FlipPlayer();
            }
        }
    }
    public void Moving() //CHECKS IF PLAYER IS MOVING & Walking & Running Animation
    {
        if (Input.GetKey("left")&&IsGrounded == true || Input.GetKey("right") && IsGrounded == true || Input.GetKey("left") && Input.GetKey("right") && IsGrounded == true || Input.GetKey("a") && IsGrounded == true || Input.GetKey("d") && IsGrounded == true || Input.GetKey("a")&& Input.GetKey("d") && IsGrounded == true)
        {
            if (Speed <= 13)
            {
                PlayerAnimation.SetBool("Moving", true);
                PlayerAnimation.SetBool("Running", false);
            }
            else if (Speed > 13)
            {
                PlayerAnimation.SetBool("Running", true);
                PlayerAnimation.SetBool("Moving", false);
            }
            
        }
        else if (Input.GetKeyUp("left") || Input.GetKeyUp("right")|| Input.GetKeyUp("a") || Input.GetKeyUp("d"))
        {
            
            PlayerAnimation.SetBool("Moving", false);
            PlayerAnimation.SetBool("Running", false);
        }
        if (IsGrounded == false)
        {
            PlayerAnimation.SetBool("Moving", false);
            PlayerAnimation.SetBool("Running", false);
        }
    }
    public void FlipPlayer() //FLIPS PLAYER HORIZONTALLY
    {
        facingRight = !facingRight;
        Vector2 localScale = gameObject.transform.localScale;
        localScale.x *= -1;
        transform.localScale = localScale;
    }
    public void SprintToggle() //Toggles Sprint [r]
    {
        if (Input.GetKeyDown("r"))
        {
            sprintToggle = !sprintToggle;
            if (sprintToggle == false)
            {
                Speed = 13;
            }
            else if (sprintToggle == true)
            {
                Speed = 30;
            }
        }
    }
    public void Jump() // JUMPS
    {
        if (Input.GetKeyDown("space")&&IsGrounded == true)
        {
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpForce);
        }
    }
    
}

Hi @abdulhadiesalama,

In your move function you have this if statement:

if (IsGrounded == true)
         {
             MoveX = Input.GetAxis("Horizontal");
             {
                 var x = MoveX * Time.deltaTime * Speed;
                 {
 
                     transform.Translate(x, 0, 0);
 
                 }
             }
             if (MoveX < 0.0f && facingRight == true)
             {
                 FlipPlayer();
 
             }
             if (MoveX > 0.0f && facingRight == false)
             {
                 FlipPlayer();
             }
         }

Because you are using Transform.Translate to move your character will immediately stop moving once you stop calling it.

This is exactly what the above if statement is doing. When you jump your bool “IsGrounded” will be set to false. This in turn stops your Transform.Translate from being called.


If you want your player to slow down in the air over time you can do serveral things.

  1. Decrease your speed multiplier while player is in air.
  2. Move your player with physics instead. (same principal as your jump)
  3. Move your player by root motion.

Root motion comes with animations. Every animation can have root motion when you animate the root of your character to move along with its animation. This can then be used in unity to move your character. If you’re interested there’s loads of tutorials and documentation on it on the internet.


To fix your problem replace the above mentioned code block with:

MoveX = Input.GetAxis("Horizontal");

var x = MoveX * Time.deltaTime * Speed;
{
      transform.Translate(x, 0, 0);
}
             

if (IsGrounded == true)
{
      if (MoveX < 0.0f && facingRight == true)
      {
           FlipPlayer();
 
       }
       if (MoveX > 0.0f && facingRight == false)
       {
           FlipPlayer();
       }
}

Or

    MoveX = Input.GetAxis("Horizontal");
    
    var x = MoveX * Time.deltaTime * Speed;
    {
          transform.Translate(x, 0, 0);
    }
                 
          if (MoveX < 0.0f && facingRight == true)
          {
               FlipPlayer();
     
           }
           if (MoveX > 0.0f && facingRight == false)
           {
               FlipPlayer();
           }

The first one still stops your character from rotating or flipping in air. The second one allows rotating or flipping aswell.

Hope this helps!

Wybren van den Akker