Inconsistent jumping(Unity 3d Engine)

I am having a problem with my code, whenever I click the q key down some times unity responds and my player jumps but then other times my player doesn’t jump at all and the engine doesn’t respond. I’ve tryed everything but I can’t find a solution.

My code:

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

public class PlayerController : MonoBehaviour
{
[Header(“References”)]
public Rigidbody rb;
public Transform head;
public Camera playerCamera;

[Header("Configurations")]
public float walkSpeed;
public float runSpeed;
public float jumpSpeed;
public float groundCheckDistance = 1.1f; // Distance for ground check

[Header("Runtime")]
Vector3 newVelocity;
bool isGrounded = false;
bool isJumping = false;

void Start()
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
    transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * 2f);

    newVelocity = rb.velocity;
    float speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
    newVelocity.x = Input.GetAxis("Horizontal") * speed;
    newVelocity.z = Input.GetAxis("Vertical") * speed;

    // Handle Jumping Input
    if (isGrounded && Input.GetKeyDown(KeyCode.Q) && !isJumping)
    {
        newVelocity.y = jumpSpeed;
        isJumping = true;
    }

}

void FixedUpdate()
{
    // SphereCast for ground detection (radius a bit smaller than capsule's)
    isGrounded = Physics.SphereCast(transform.position, 0.4f, Vector3.down, out RaycastHit hit, 1.1f);

    // Apply movement
    rb.velocity = transform.TransformDirection(newVelocity);

    // Reset jump if grounded
    if (isGrounded)
    {
        isJumping = false;
    }
}


void LateUpdate()
{
    Vector3 e = head.eulerAngles;
    e.x -= Input.GetAxis("Mouse Y") * 2f;
    e.x = RestrictAngle(e.x, -85f, 85f);
    head.eulerAngles = e;
}

public static float RestrictAngle(float angle, float angleMin, float angleMax)
{
    if (angle > 180)
        angle -= 360;
    else if (angle < -180)
        angle += 360;

    if (angle > angleMax)
        angle = angleMax;
    if (angle < angleMin)
        angle = angleMin;

    return angle;
}

}

Your code specifies that pressing the Q key on it’s own is not enough to trigger a jump. isGrounded must also be true and isJumping must be false. If you press Q and nothing happens then check isGrounded and is Jumping. Maybe one of these is not being set properly?

using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("References")]
public Rigidbody rb;
public Transform head;
public Camera playerCamera;

[Header("Configurations")]
public float walkSpeed;
public float runSpeed;
public float jumpSpeed;
public float groundCheckDistance = 1.1f; // Distance for ground check

[Header("Runtime")]
bool isGrounded = false;
bool isJumping = false;

void Start()
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}

void Update()
{
    rb.MoveRotation(Quaternion.Euler(0,Input.GetAxis("Mouse X")+transform.eulerAngles.y,0)); // rotate a rigidbody with MoveRotation to prevent jitter
    
    // Handle Jumping Input
    if (isGrounded && Input.GetKeyDown(KeyCode.Q))
        rb.AddForce(transform.up * jumpSpeed, ForceMode.Impulse); // we can get away with using AddForce in Update if we're using ForceMode.Impulse

    Vector3 e = head.eulerAngles;
    e.x -= Input.GetAxis("Mouse Y") * 2f;
    e.x = Mathf.Clamp(-Mathf.DeltaAngle(e.x,0),-85f,85f); // Clamp
    head.eulerAngles = e;
}

void FixedUpdate()
{
    // SphereCast for ground detection (radius a bit smaller than capsule's)
    isGrounded = Physics.SphereCast(transform.position, 0.4f, Vector3.down, out RaycastHit hit, 1.1f);

    float speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
    Vector3 newVelocity = new Vector3(Input.GetAxis("Horizontal") * speed,rb.velocity.y,Input.GetAxis("Vertical") * speed);
    rb.velocity = transform.TransformDirection(newVelocity);
}

}

I made a few changes. You shouldn’t really set the velocity of a rigidbody and instead you should use AddForce or AddRelativeForce. But you may have done it because you want to stop on a dime and so I’ve left it in. But if you’re okay with some inertia then switch to using AddRelativeForce.