Side Scrolling camera ( Megaman X6 Like )

Hey!

So im trying to replicate a camera similar to what youre seeing in the above GIF

I got most of the hard work done as in, Im alredy checking how " upwards " or downwards the player is from the camera and scroll either up / down. However the method i have a few troubles. But first ill explain how its working

By default the camera doesnt move on the Z axis , it only moves when the player reaches either the maxium height, or goes way too much “below” the camera .

Im projecting the player world location to screen coordinates and then dividing it by the viewport size so i get consistent results regardless of the viewport size. Using that

Then i compare the Y value if its above a specific number ( in my case when the player is somewhat near the upper edge, 0.600 ) i start the scrolling ( Interpolate camera to the player Z plus a few units more so it goes upwards while following him ). Once thats done i start checking the Y value for excess ( The more away he is from the upper edge the higher the value becomes ) so at Arround 0.800 i disable the scrolling since it means the player isnt in the scrolling height anymore…

Then i set the camera Z value to the last player Z on ground

This works fine. however it really messes up when the player:Jumps, Reaches maxium heigh. Then goes below the height ( since the camera follows him ) and then lands on another platform. Since its set to use the LAST player Z on ground the camera goes below the player. and then snaps back once the player lands on the ground as you can see here

Any help is greatly appreciated. Thanks!