How do I make the player fall when it hits the ceiling?

I followed Brackeys tutorial for a FPS player controller but found that I float for about half a second when i hit a ceiling. If someone can help it would be much aprreciated. here is the script.

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

public class playerMovement : MonoBehaviour
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

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

    Vector3 velocity;
    bool isGrounded;

    // Start is called before the first frame update
    void Start()

    // Update is called once per frame
    void Update()
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, 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);


It is probably because velocity.y is still positive from when the jump happened and it takes time for gravity to pull it down to 0, while that is happening you slide along the ceiling. If you want to immediately go down you should do velocity.y=0 on impact with the ceiling.

If the controller has a RigidBody, you could also affect that with forces to get much for free, instead of setting velocity manually.

That makes sense. lol I’m new to unity and C# so how should I add that in? I just followed Brackeys tutorial so I don’t fully understand everything I made. XD