Descent ship align z axis to underneath plane normal

I have a question that was asked before.

Here

I implemented the following code into my spaceship:

    RaycastHit hitInfo;
    if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, 40.0f))
    { Quaternion rot = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
     
    transform.rotation =
    Quaternion.Slerp (transform.rotation,rot, Time.time * centeringSpringForce);
     
    }

Right now, the code will align and recenter my Z axis to the normal of the polygon underneath my ship, but it also freezes my ships ability to be rotated in the other axis. It freezes my Y and Z rotation to 0 (or very close to 0), and perfectly locks my ship to the z value of the normal underneath my ship. I’ve been scratching my head for a little while now, do you have any ideas for what might be causing my ships X and Y rotation to be frozen at around zero?

1 Answer

1

Try the following change to how you calculate ‘rot’:

Quaternion rot =  Quaternion.FromToRotation(transform.up, hitInfo.normal) * transform.rotation;

Consider also the order the scripts are run.