Problems Jumping with third person character

Hi, basically I have a character controller that makes my character move and always have the way the camera faces set as forward. for some reason when I try jumping it doesn’t let the character off the ground, he just bounces for a millisecond. This is what I have right now:

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

public class ThirdPersonMovement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;

    public float speed = 6f;

    public Animator anim;

    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    public float gravity = -9.81f;

    public Transform groundCheck;
    public float groundDistance = .4f;
    public LayerMask groundMask;

    public float jumpHeight = 3f;

    Vector3 velocity;

    bool isGrounded;

    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded)
        {
            velocity.y = -2f;
        }
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical);

        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);

            anim.SetBool("running", true);
        }
        else
        {
            anim.SetBool("running", false);
        }

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            direction.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        
        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);

    }

}

Just a wild guess, but I would have to assume that your gravity is kicking in, since you’re constantly setting:

 velocity.y += gravity * Time.deltaTime;

which is -9.81f so whenever you try to jump the gravity just pulls you down again, so you might need to disable it while jumping or look into a solution that works with the gravity together, so that the jump can “overpower” the gravity for a set amount of time before it pulls the Player down again.

Welp turns out the solution is I am really dumb. I had

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

so it was constantly putting my velocity to -2. the bump I was seeing was the 1 frame where my velocity changed to the new one before returning to -2. The fix I should have had was

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

So that it only changed after it was back on the ground.