How do I limit camera height so it doesn't collide with the ceiling?

When the camera collides with low ceilings it compresses the distance to the player (zooms in), which causes some jarring camera motion in tight spaces, especially when jumping. I tried messing with lag but it’s only a half-solution.

I feel like I just need to limit the camera world height to just below the ceiling so it doesn’t collide and shoot forward. My instinct is to use a line trace up from the camera to detect when it gets close and then “freeze” the z axis. When I try it I get clipping, weird positioning, etc. And I still have to figure out how to “unfreeze” it properly. This is what I have so far. Am I chasing a feasible solution or is there a better way?