Camera Orbit for spaceship

There is a lot of help for orbital cameras. And the best one if of course provided by unity wiki MouseOrbitImproved.

I am trying to use this script for a spaceship that can also rotate on its z axis.

This is is how i have my objects set up.

-Spaceship

-FlyCam(GameObject)

–FlyCamera(Camera)

The FlyCamera has a script that tells the parent FlyCam to follow the spaceship if the spaceship moves.

I added the MouseOrbitImproved script to FlyCamera and on start it is disabled. If I press and hold Left Control, I can orbit the spaceship. When I release Left Control, x and y get reset back to 0. Everything works well if I fly in a straight line and the script is active.
If I let got of Left Control and change my y or z axis, and activate the script, the camera still orbits the object, but not using the correct axis.

Initially, This is what it looks like if I fly in a straight line and I don’t change direction.

I can activate the orbit script and it will orbit correctly.
Now if I disable and I continue to fly, but this time I change direction and go straight up 90 degrees. If I activate the orbit script, it will look like this.

This is not how it should be though, it should be like in the first image.

Is there any way I can always keep the orbital camera behind the spaceship like in the first image every time i activate the script?

If there is any resource I may have missed that already answers this question, I’m sorry. I tried to search before I asked, but i could not find a solution that involved the z axis. they all deal with only x and y axis assuming the object’s y axis is always up.

Any help would be appreciated. Thank you in advance.

For those of you who find this question. I’ll redirect you to where i found a good solution to the question.

How to make camera rotation relative to target.