no x movement in the air (while jumping)

I jump and the character just goes straight up. the sprite still looks left and right when i press a and d

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

public class Player : MonoBehaviour
{
//Setting up engines, animation, speed, and whatnot
private Rigidbody2D rb;
private Animator anim;
public int crystals = 0;
//Animation
private enum State {idle, running, jumping, falling} 
private State state = State.idle;
private Collider2D coll;
[SerializeField] private float speed = 200f;
[SerializeField] private float jumpForce = 25f;
[SerializeField] private Text crystalText;

[SerializeField] private LayerMask ground; //collider
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "collectable")
{
Destroy(collision.gameObject);
crystals+=1;
crystalText.text = crystals.ToString();
}
}
private void Start()

//Engines used
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
coll = GetComponent<Collider2D>();
}
private void Update()
//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))
{
rb.AddForce(new Vector2(0,jumpForce), ForceMode2D.Impulse);
state = State.jumping;
}
VelocityState();
anim.SetInteger("state", (int)state);
}
}

Do you have an actual question or problem?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

If it is a problem with the code above, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

I’m taking a wild guess in your VelocityState() method you are messing with velocity on the X axis when state == State.jumping. But you are keeping that code to yourself, so I’m just left to guess.