I created this code based on a video and I don t know why the player cannot always jump

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

public class playermouvement : MonoBehaviour
{
      public CharacterController controller;
    //gravity
      public float speed = 12f;
      public float  gravity = -9.81f;
      public float  JumpHeight = 3f;
     //ground check
      public Transform groundCheck;
      public float groundDisdance = 0.4f;
      public LayerMask groundMask;
    
      Vector3 velocity;
      bool isGrounded;

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

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

      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);

    }
}

Put some debug lines in your code and see what is happening, e.g.

Debug.Log("Just before check, velocity: " + velocity.y + " isGrounded: " + isGrounded);

I would guess you have some noise in velocity.y that is preventing it firing or that your ground check distance is too small, but you don’t give much information. Use the debug lines to understand what your code is doing.

If you want better help here, generate a simple reproducer of your problem with a 1x1x1 cube on a flat surface, or whatever type of surface you need (you don’t say) to cause the problem. Give a good description of when the problem occurs and what you have tried already.

thanks I actually resolved the problem