Basis "rotation wrapping" issue

Godot Version

v4.4.1.stable.mono.official

Question

Hi, I was working on a camera follow script and I use the GlobalBasis.Z of the following target to determine the camera rotation with Basis.LookingAt().

The problem is if the target.GlobalBasis.Z exceedes 180 degrees on the X-Axis, up or down, the camera realigns itself to the horizon, which is unintended behaviour.

I’m aware that this is just how rotation works in Godot, and this wouldn’t be a huge problem at ALL if it affected only the camera… but I do use the GlobalBasis.Z for other things that use Cross() and they also have the same exact problem. Here’s the relevant code that causes trouble:

private Node3D cam;

private Node3D ship;
private ShipReferences references; //stores all data from the ship

private const byte rotationTime = 4;

private void Rotation(float delta)
{
    Vector3 shipDir = ship.GlobalBasis.Z; //forward vector, also the bane of my existance.

    float ahead = Mathf.Max(references.Velocity.LinearVelocity, rotationTime); //get the ship's current velocity
    cam.GlobalBasis = cam.GlobalBasis.Orthonormalized().Slerp(
        Basis.LookingAt(shipDir * ahead), //(ship.GlobalPosition - cam.GlobalPosition) has the same exact issue as shipDir
        1 - Mathf.Exp(-(ahead * 2.72f) * delta)
    );
    GD.Print(shipDir); //x value goes back and forth?
}

There’s some posts that kinda mention this issue, but their solution doesn’t seem to apply to this (i think):

Any help would be greatly appreciated as after so much time looking for answers online, this is my last resort. Thanks.

I think I know what the problem really is; it’s the way I’m trying to use Basis.LookingAt. I realize that I haven’t specified what I want to do very well, but the idea is that if the ship gets upside down, the camera also has to be upside down. I think the looking at function probably does exactly what it implies: “Just look at the thing, I don’t care how, just do it.”

The work around that immediately comes to my head is very convoluted but basically, copy the X rotation of the ship and then apply the Y and Z of Basis.LookingAt, but again, it feels very wrong to do this. So now I’m still looking for alternatives to the way I have the rotation setup. Anything that doesn’t feel as hacky as this.