Think about how a helicopter moves.
You are hovering in place. play with the controls so the copter tilts forward, and it starts to move forward. If you didn’t give it more power (more collective for more blade “bite”) it would also start to go down. The “force vector” of the rotor blades is an angle, and the vertical component of the vector is what keeps the copter up, the horizontal component of the vector moves the copter in the forward direction.
At a certain angle, there is not enough vertical component to keep the copter at the same altitude and it will start to go downward. Of course, trying to gun it enough to stay up is probably also creating a massive forward push… say 45 degrees.
It’s pretty simple, actually - the rotor is a propeller pushing the copter (in general) in the direction perpendicular to the rotor, i.e. along the direction of the shaft. The same applies at any angle - The rotor is agnostic, it does not care which way the cockpit is pointed, just which way the controls are forcing it. Left, right, backwards, forwards - same idea.
It is impossible to maintain lift beyond a certain angle.
Many helicopter tricks, as mentioned, rely on momentum. Follow one of the links and watch a few more of YouTube helicopter tricks. There’s one where the helicopter goes vertical sideways. What it actually does - it’s going forward fast, then tilts and its path curves upward until it’s almost 90 degrees to the ground - then back down again. What it’s doing is using the momentum, much as if it was starting a loop. As it tilts sideways and goes up, the rotor is pushing against the forward momentum; when it runs out of forward momentum, it starts to fall back and the pilot controls it back from sideway to horizontal going in the other direction.
The only way to actually do the trick OP describes is to tilt the craft hard over onto its side while killing any collective/lift (so it doesn’t smash into the wall above the pilot’s head) then once through the gap, hopefully there was a hundred feet or two of room to fall until the craft is horizontal again. power is applied and it can get decent lift. While sideways, it’s a flying block of stuff and will simply fall downward. There is not vertical support. As with most truly lethal stunts, CGI is better provided it doesn’t look like Roadrunner cartoon physics.