Object's rotation is messed up whenever it's x reaches 90

I am very new to Unity, and I am making a game where I need to be able to fly an airplane. I’ve been implementing the roll and yaw of the aircraft, and everything works, except when the x of the rotation reaches 90 - then the object is instantly rotated in all axes in a very strange way, and I can’t tell what part of my code does this. If you have the time to help, it would probably be good to apply the following code to a cube or something to see what I mean. Many thanks for any help!

public class PlayerMovement : MonoBehaviour
{
    // Variables controlling roll of the aircraft; 
    private Vector3 rollRotation;
    [SerializeField] private float rollSpeed = 100;

    // Variables controlling yaw of the aircraft; 
    private Vector3 yawRotation; 
    [SerializeField] private float yawSpeed = 25; 
    

    private void FixedUpdate()
    {
        PlayerRoll(); 
        PlayerYaw(); 
    }

    private void PlayerYaw()
    {
        if (Input.GetKey(KeyCode.A))
        {
            yawRotation = Vector3.down; 
        }
        else if (Input.GetKey(KeyCode.D))
        {
            yawRotation = Vector3.up; 
        }
        else
        {
            yawRotation = Vector3.zero;
        }

        Vector3 finalYawRotation = yawRotation * yawSpeed * Time.deltaTime;
        Vector3 rot = transform.rotation.eulerAngles + finalYawRotation; 
        rot.y = ClampAngle(rot.y,-20f,20f); 
        transform.localRotation = Quaternion.Euler(rot);  

        float ClampAngle(float angle, float from, float to)
        {
            if (angle < 0f) angle = 360 + angle;
            if (angle > 180f) return Mathf.Max(angle, 360 + from);
            return Mathf.Min(angle, to);
        }
    }
    private void PlayerRoll()
    {
        // Checks which direction to apply roll, if any, and then applies it; 
        if (Input.GetKey(KeyCode.Q))
        {
            rollRotation = Vector3.left;
        }
        else if (Input.GetKey(KeyCode.E))
        {
            rollRotation = Vector3.right;
        }
        else
        {
            rollRotation = Vector3.zero;
        }

        Vector3 finalRollRotation = rollRotation * rollSpeed * Time.deltaTime; 
        Vector3 rot = transform.rotation.eulerAngles + finalRollRotation; 
        transform.localRotation = Quaternion.Euler(rot);

    }
}

The issue you encounter here is a really really really common one for which i don’t understand why Unity does not emphazise this issue in their tutorials.

Short version: Any rotation always has multiple representations as euler angles. (not counting that (180,0,0) would be the same as (180+360,0,0))

The only way to ensure consistent behaviour is by using quaternions as a quaternion is always unique for the representation of a rotation.

As unity always calculates rotations using quaternions and all euler angles are just used to make things readable for us humans, the result can thus jump from one representation to another when certain angle thresholds are reached.

That being said: there are hundreds of questions on this platform which solve the issue for clamped rotations. Please try to find a solution for this in there. If you are stuck with something specific, feel free to return and let us know.