How to get the camera to flip upside down at the right time.

Godot Version

4.5.1

Question

I’m looking for some general advice for how to solve a problem of wanting the camera to go upside down when the player is upside down.
Working on what I’ll loosely describe as a Sonic 3d clone with C# scripting.
The issue I’m running into is going into a corkscrew/loop-de-loop where the player can run all the way through a loop. Running through the loop is fine, I use the wall normal of the colliding surface to rotate the player model and stick them to the surface of the wall as well as translate the velocity into a sensible angle.
The issue I’m running into is when the camera flips itself right side up and I don’t want it to.
The camera is attached to a node3d object that follows the player.
And when the player’s rotation (In degrees) on X is about -123 it looks correct.
When I hit the next polygon in the corkscrew and the rotation shifts to X: -146 the camera flips itself right-side up. I can compensate for this by calling LookAt with:

	LookAt(Player.Position, new Vector3(0, -1, 0));

But I’m having trouble coming up with a good mathematical test to figure when LookAt is going to invert the camera.
I want to be able to run up a wall, curve onto the ceiling and because the player is upside down, keep the camera upside down.

Any suggestions for how to tackle this?
My next step was going to measure the angles between the camera and the players and manually rotating the camera into position, although the math is making my head hurt a bit. (Maybe I should start by learning how basis works?)

Some images to help illustrate the issue.

Running up the curve, everything is fine. Player X angle is -123

Then I move a little more, and the camera flips itself right-side up, which is confusing.
I’m guessing the actual flip point is at -135, but my polygon model is a bit simple here.

But telling LookAt to invert the camera does provide a correct view and you can just press “forward” on the stick to run through

This is the point on the corkscrew that the camera flips itself back up

I just can’t seem to come up with a good way to tell when to tell Godot to flip the camera based off the angle of the player. Any suggestions?

Thank you for your time on this.

Yes, start there.

OMG the Basis solution was darned easy. Posting this here in case anyone else runs into a similar issue. I use Basis to aim the camera, then use the players rotational basis to set the camera correctly. This line of code works perfectly to follow the player through a loop and keep the camera sane:

PlayerCamera.Basis = PlayerCamera.Transform.LookingAt(Player.Position,Player.Basis.Y).Basis;

Thank you, you amazing genius programmers at Godot. I’m astounded how it just works.