My character is able to jump an infinite amount of times

I only want my character to be able to jump when its on the ground. Sorry if this is simple. I am new to both unity and c#. This is my code: `using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

public float speed = 18f;

public float jumpHeight = 10f;

private Rigidbody rb;

// Use this for initialization
void Start ()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void FixedUpdate ()
{
    float horizontalAxis = Input.GetAxis("Horizontal");
    float verticalAxis = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(horizontalAxis, 0, verticalAxis) * speed * Time.deltaTime;

    rb.MovePosition(transform.position + movement);

    if (Input.GetKeyDown("space"))
    {
        transform.Translate(Vector3.up * jumpHeight * Time.deltaTime, Space.World);
    }
    
}

 void Update()
 {
    if (Input.GetKeyDown("space"))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jumpHeight;
    }
 }

}
`

Use an if statement that if the player is on the ground, you can jump. If in the air, don’t jump. Like this:

private bool isGrounded;
void Update(){
    if(Input.GetKeyDown(KeyCode.Space)){
        if(isGrounded){
            //jump
        }
    }
}

void OnCollisionEnter(Collider other){
    if(other.tag == "ground"){
        isGrounded = true;
    }
}

void OnCollisionExit(Collider other){
    if(other.tag == "ground"){
        isGrounded = false;
    }
}

You have to make whether you are standing on the ground.
in character controller you have method named isGrounded()

Your code is a bit messy, so I would recommend to clean that up a bit, but anyways, I would recomend to take a look at the FPS Controller and Rigidbody Controller scripts in the Standard Assets pack, you can use those as a basis to learn how to make a character learn how to jump or alternatively you could just use those controllers yourself and modify them a little bit to suit your needs.

To download the Standard Assets pack, go to the Asset Store window (it’s right next to your scene and game view windows if you are on the default layout) and search “Standard Unity Assets” and download the one with a thumbnail of untextured prefabs, then import whatever you want from that pack, like the Rigidbody Controller and FPS controller.

Alternatively, you could visit tutorials of how to make FPS controllers on YouTube like this one:

@Platiniumekk I tried to add what you said but its telling me that i need to add a CharacterController to my player. When i tried to do that the movement got really buggy and laggy and i wasnt able to jump at all. Is there any way to fix this without the CharacterController?