why do i stop falling when i stop pressing w

i got this from a YouTube tutorial and it wont work, i checked and i wrote everything the same as in the video

float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
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);
    Cursor.lockState = CursorLockMode.Locked;
    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);

It is because your code for gravity is inside an if statement which only runs if you are moving. Just take
velocity.y += gravity * Time.deltaTime; out of the if statement and put it in update instead. The only problem is that doing this will constantly increase velocity so it will never reset. The tutorial you followed did not account for this. I suggest this tutorial instead:

@VoidPhoenix96 ok thanks