(2d) cant go left or right in the air after touching ground

made a video for the demonstration

when i first enter the game, i CAN move left or right in the air, but after i hit the ground, once im in the air, x movement is no longer a possibility. I didnt show me going left, but i cant go left in the air either.
line 74 is where the problem should lie. the Debug.Log on line 73 does go off when I press Space, so thats not a problem.
i need help :confused:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
//Setting up engines, animation, speed, and whatnot
    private Rigidbody2D rb;
    private Animator anim;
    private Collider2D coll;
   
//FSM
    private enum State {idle, running, jumping, falling, hurt}
    private State state = State.running;

//Inspector Variables
    [SerializeField] private float speed = 500f;
    [SerializeField] private float jumpForce = 80f;
    [SerializeField] private int crystals = 0;
    [SerializeField] private TextMeshProUGUI crystalText;
    [SerializeField] private LayerMask ground;
    [SerializeField] private float hurtForce = 1000f;
    [SerializeField] private AudioSource crystal;
    [SerializeField] private AudioSource footstep;
    [SerializeField] private int health;
    [SerializeField] private Text healthAmount;
    private Vector2 velocity;

    private void Start()
   
//Engines used
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        coll = GetComponent<Collider2D>();
        healthAmount.text = health.ToString();

    }
    private void Update()
   
//If hurt
    {
        if(state != State.hurt)
        {
            Movement();
        }
        AnimationState();
        anim.SetInteger("state", (int)state);
    }
//Movement
    private void Movement()
   
    {
        float hDirection = Input.GetAxis("Horizontal");
    //Left
        if (hDirection < 0)
        {
            rb.AddForce(new Vector2(-speed, 0), ForceMode2D.Impulse);
            gameObject.GetComponent<SpriteRenderer>().flipX = true;
        }
    //Right
        else if (hDirection > 0)
        {
            rb.AddForce(new Vector2(speed, 0), ForceMode2D.Impulse);
            gameObject.GetComponent<SpriteRenderer>().flipX = false;
        }
    //Jumping
        if (Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
        {
            Debug.Log("yo mama");
            rb.AddForce(new Vector2(speed, jumpForce), ForceMode2D.Impulse);
            state = State.jumping;
        }
    }

//Entities
    private void OnTriggerEnter2D(Collider2D collision)
    {
//Collect crystals
        if (collision.tag == "collectable")
        {
            crystal.Play();
            Destroy(collision.gameObject);
            crystals +=1 ;
            crystalText.text = crystals.ToString();
        }
        if(collision.tag == "powerup")
        {
            Destroy(collision.gameObject);
            jumpForce = 100f;
            GetComponent<SpriteRenderer>().color = Color.yellow;
            StartCoroutine(ResetPower());
        }
    }
    private void OnCollisionEnter2D(Collision2D other)
    {
//Stomping on Enemies
        if (other.gameObject.tag == "Enemy")
        {
            Enemy enemy = other.gameObject.GetComponent<Enemy>();
            if (state == State.falling)
            {
                enemy.JumpedOn();
                rb.AddForce(new Vector2(speed, jumpForce), ForceMode2D.Impulse);
            }
            else
            {
                state = State.hurt;
                HandleHealth();
                if (other.gameObject.transform.position.x > transform.position.x)
                {
                    rb.velocity = new Vector2(-hurtForce, rb.velocity.y);
                }
                else
                {
                    rb.velocity = new Vector2(hurtForce, rb.velocity.y);
                }
            }
        }
    }
    private void HandleHealth()
    {
        health -= 1;
        healthAmount.text = health.ToString();
        if (health <= 0)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }

    //Animation   
    private void AnimationState()
    {
//Jumping
        if(state == State.jumping)
        {
            if(rb.velocity.y< .1f)
            {
                state = State.falling;
            }
        }
//Falling
        else if(state == State.falling)
        {
            if(coll.IsTouchingLayers(ground))
            {
                state = State.idle;
            }
        }
//If below speed when hurt, Idle
        else if (state == State.hurt)
        {
            if(Mathf.Abs(rb.velocity.x) < .1f)
            {
                state = State.idle;
            }
        }
//If above speed, Run.
        else if(Mathf.Abs(rb.velocity.x)>1f)
        {
            rb.AddForce(new Vector2(rb.velocity.x,0));
            state = State.running;
        }
//Nothing else executes, Idle
        else
        {
            state = State.idle;
        }
       
    }
    private void Footstep()
    {
        footstep.Play();
    }
    private IEnumerator ResetPower()
    {
        yield return new WaitForSeconds(5);
        jumpForce = 80f;
        GetComponent<SpriteRenderer>().color = Color.white;
    }
}

I would replace the line 74 with the following:
rb.AddForce(new Vector2(Mathf.Sign(hDirection) * speed, jumpForce), ForceMode2D.Impulse);

1 Like

Yes, that’s an issue. When you jump, you’re always using “speed”, when in some cases it should be -speed, based on direction. This will push your character in one direction. You can also use a 0 for the X value in the jump vector, just use the Y (jumpForce) and see if you can move in the air better.