title: How can I check if my 2D character is in the air in Unity2D?
body: <p>So I'm trying to make my 2D character jump.
  If I'm running on the ground it works perfectly, but when I collide with a wall, it gets buggy: I can jump even in the air.
  I've tried solving this with a bool variable, but it didn't work.
  I thought about checking whether the player is in the air and then setting the is_on_ground variable to false.
  Obviusly, the player should only jump if it's on the ground.
  My code is the following:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class PlayerMovement : MonoBehaviour
  {
      private bool canJump = false;
      public SpriteRenderer sr;
      private bool facingRight;
      private Animator animator;
      private bool is_on_ground;
      public Button leftBtn;
      public Button rightBtn;
      public Button jumpBtn;
      public float max_velocity;
      public float velocity;
      public float jump_scalar;
      private Rigidbody2D rb;
      private float x_movement;
      private Vector2 movement;
      void Start() {
          canJump = true;
          facingRight = true;
          animator = gameObject.GetComponent&lt;Animator&gt;();
          leftBtn.onClick.AddListener(moveLeft);
          rightBtn.onClick.AddListener(moveRight);
          jumpBtn.onClick.AddListener(jump);
          rb = gameObject.GetComponent&lt;Rigidbody2D&gt;();
      }
      private void FixedUpdate() {
          if (rb.velocity.magnitude &lt; max_velocity) {
              movement = new Vector2(x_movement, 0);
              rb.AddForce(movement * velocity);
          }
      }
      private void OnCollisionEnter2D(Collision2D collision) {
          if (CollisionIsWithGround(collision)) {
              animator.SetBool("isJumping", false);
              canJump = true;
          }
          if (collision.collider.tag == "wall") {
              canJump = false;
          }
          is_on_ground = true;
  
      }
      private void OnCollisionExit2D(Collision2D collision) {
          if (!CollisionIsWithGround(collision)) {
              is_on_ground = false;
          }
          if (collision.collider.tag == "wall") {
              is_on_ground = true;
              canJump = true;
          }
      }
      private bool CollisionIsWithGround(Collision2D collision) {
          bool is_with_ground = false;
          foreach (ContactPoint2D c in collision.contacts) {
              Vector2 collision_direction_vector = c.point - rb.position;
              if(collision_direction_vector.y &lt; 0) {
                  is_with_ground = true;
              }
          }
          return is_with_ground;
      }
      public void moveLeft() {
          if (is_on_ground) {
              animator.SetBool("isJumping", false);
          }
          animator.SetBool("isRunning", true);
          sr.flipX = true;
          facingRight = false;
          x_movement = -1;
      }
      public void onRelease() {
          animator.SetBool("isRunning", false);
          x_movement = 0;
          if(is_on_ground) {
              animator.SetBool("isJumping", false);
              rb.velocity = Vector3.zero;
          }
      }
      public void moveRight() {
          if (is_on_ground) {
              animator.SetBool("isJumping", false);
          }
          if (!facingRight) {
              sr.flipX = false;
              facingRight = true;
              animator.SetBool("isRunning", true);
          } else {
              animator.SetBool("isRunning", true);
          }
          x_movement = 1;
      }
      public void jump() {
          if(is_on_ground) {
              if (canJump) {
                  animator.SetBool("isJumping", true);
                  Vector2 jumpForce = new Vector2(0, jump_scalar * 100);
                  rb.AddForce(jumpForce);
              }
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YQrmU.png" rel="nofollow noreferrer">the problem looks like this</a></p>