Why isn't my code for jumping working?

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

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;

public float speed = 12f;
public float gravity = -19f;
public float jumpheight = 3f;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;
bool isGrounded;

// Update is called once per frame
void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
        velocity.y = -1f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);

}

}
Why isn’t it working? The movement part is working fine but I can jump in mid air for some reason.

It’s a first person 3D game. Forgot to add that.

if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}

I followed the tutorial and it said that it was important to add -2f and not -1 or 0 because the CheckSphere function can miss detecting the collision and possibly not trigger the isGrounded to be false.

2nd possibility:
Did you set up the LayerMask in the inspector to register the ground layer.
Send me a screenshot whenever you can.