How do I stop infinite jumping in air?

Hello, I’m extremely new to Unity and coding as a whole, as I’m having some problems here (using C#). Whenever I jump, this is a 2D sidescroller, it works well and exactly how I want it to. However, if I continue to press the spacebar, then the character continues to jump in air. This creates a flying effect that I don’t want, how do I fix this? Heres my script for the player.

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

public class Player : MonoBehaviour {

public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public float jumpSpeed = 8f;
private Rigidbody2D rigidBody;

public bool grounded;

private Rigidbody2D rb2d;
private Animator anim;

void Start () 
{
	rb2d = gameObject.GetComponent<Rigidbody2D> ();	
	anim = gameObject.GetComponent<Animator> ();
}

void Update ()
{
	{
	anim.SetBool ("Grounded", grounded);
	anim.SetFloat ("Speed", Mathf.Abs (Input.GetAxis ("Horizontal")));

	if (Input.GetAxis ("Horizontal") < -0.1f)
		transform.localScale = new Vector3 (-1, 1, 1);

	if (Input.GetAxis ("Horizontal") > 0.1f) {
		transform.localScale = new Vector3 (1, 1, 1);
	}
		
	if (Input.GetKeyDown (KeyCode.Space) && grounded) {
		rb2d.AddForce (new Vector2 (0, jumpPower));
	} 
			

	}
}

	void FixedUpdate ()
{
	{

		float h = Input.GetAxis ("Horizontal");
		//Moving the player
		rb2d.AddForce ((Vector2.right * speed) * h);

		//Limiting the speed of player
		if (rb2d.velocity.x > maxSpeed) {
			rb2d.velocity = new Vector2 (maxSpeed, rb2d.velocity.y);
		}

		if (rb2d.velocity.x < -maxSpeed) {
			rb2d.velocity = new Vector2 (-maxSpeed, rb2d.velocity.y);
		}
	}
}

}

So there 2 options that I know of:
#1
This will only work if the ground Y-axis is the same throughout the whole game, so if there are platforms the player can jump on, DON’T DO THIS ONE! In an if statement that makes the player jump, add this: "If the player is on (y-Axis of the ground), then it can jump, else, it can’t.
#2
Use OnColliderStay/OnColliderStay2D. Tag all your ground/platform (wherever the player can jump) to a tag, such as “ground” Check with the OnColliderStay to see if the player is on the ground, such as:

void OnColliderStay (Collider other) {
      if (other.gameObject.tag == "Ground") {
            canJump = true;
       }
}

}

and yes, make a bool to see if canJump is false/true.
I hope this helped!

I think the problem is your “grounded” variable. Since it’s define, the value never be set, I know you use it to control your animation but you also use it to check if player is grounded or not. But in your code this var is always false (default value of bool) in logic side your code will not work since grounded always is false but as you said now it’s work fine, so I think maybe I missed something…Hmm. It has 2 ways to fix this:

  1. Add some check to change your grounded var state.

  2. Do as Carson365 commend, define one more bool var to check the state when player grounded and change this code if (Input.GetKeyDown (KeyCode.Space) && grounded) { rb2d.AddForce (new Vector2 (0, jumpPower)); }
    to this
    if (Input.GetKeyDown (KeyCode.Space) && canJump) { rb2d.AddForce (new Vector2 (0, jumpPower)); }

I prefer the 2, because you should not use a state check var to set parameter in animator, since you will use this check var various times, so in future when you want to do something else when your player grounded after jump, it’s maybe affect the animation and you will messed with other bugs or problems. So just split it since the purpose of this vars is not the same.

Hope this help. Cheers!!! Vince

mine doesnt work my sprite can still jump in the air

To stop your sprite from jumping twice or more you add a bool
here is my code

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

public class PlayerController : MonoBehaviour
{
private Rigidbody playerRB;
public float jumpForce = 10;
public float gravityModifier;
public bool isOnGround = true;
// Start is called before the first frame update
void Start()
{
playerRB = GetComponent();
Physics.gravity *= gravityModifier;
}

// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.Space) && isOnGround)
    {      
        playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        isOnGround = false;
    }
}
private void OnCollisionEnter(Collision collision)
{
    isOnGround = true;
}

}