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):
- This one mentions it briefly, but the solution might be a work-around.
- This seems to fix it but I don’t know how to apply it.
Any help would be greatly appreciated as after so much time looking for answers online, this is my last resort. Thanks.