Player sticks to ceiling - character controller

Hello, I’m using the character controller component for a 2D platformer. When I Jump and when my player hits the ceiling while moving it kind of sticks to the ceiling.

this is my move function:

void Move()
	{
		
		if(controller.collisionFlags == CollisionFlags.Above)
		{
			jumpCount = maxJumpCount;
			verticalVelocity = moveDirection.y;	
		}
		
		moveDirection = transform.TransformDirection(moveDirection);
		
		moveDirection *= moveSpeed;
			
		moveDirection = new Vector3(moveDirection.x, verticalVelocity, moveDirection.z);
		
		
		moveDirection = new Vector3(moveDirection.x, (moveDirection.y - gravity * Time.deltaTime), moveDirection.z);
		if(controller.isGrounded && moveDirection.y < -1)
			moveDirection = new Vector3(moveDirection.x, (-1), moveDirection.z);
		
		controller.Move(moveDirection * Time.deltaTime);
		
	}

verticalVelocity is set upon pressing the jump button.

When the player jumps and hits its head it comes down immediately, it only happens while moving.

EDIT: The moveDirection is set to Vector3.zero at the start of the update.
When I print the vector before setting the verticalVelocity, the y-value is always 0, even when I move while hitting the ceiling. The character is just not coming down.

Your problem is with the character controller. Set the “Step Offset” option to zero in order to make your character no longer stick. If you still want the character to work with small steps, I suggest hacking it with transparent textures or trying different smaller values in Step Offset, whatever works for you.

verticalVelocity = moveDirection.y;

“If the player hits their head, move the player at their current velocity”?

Don’t you want to set that to zero instead (or set MD.y to zero first)?