CharacterController does not transform as expected

This is a question that I’ll be answering myself just because it caused so much trouble that I want a resource out there for other people who run into the same problem.

I have a character with an Animator (with root motion) and the CharacterController component provided by Unity. I want to flip it upside down so that the top of its head is touching the same point. So, a 180 degree rotation on the facing axis around the top of its head.

The character is 1 unit tall, so the Vector3.up below is just a shorthand for a point, it’s not a vector.

if( Input.GetKeyDown( KeyCode.Space ) )
{
    Vector3 rotateAround = transform.localToWorldMatrix.MultiplyPoint( Vector3.up );
    Matrix4x4 flip = CreateRotationMatrixAroundPoint( rotateAround, Vector3.right, 180f );
    Quaternion flipRot;
    ExtractRotationFromMatrix( flip, out flipRot );

    transform.position = flip.MultiplyPoint( transform.position );
    transform.rotation = flipRot * transform.rotation;
}

Instead of the feet appearing at twice his height with his head down. The feet are appearing at twice his height with his head up.

If I was moving a normal object, the code above would work. However, with the Character Controller, the order in which the position and rotation are changed makes a difference. Simply place the rotation code above the position code and you get the expected behaviour.

if( Input.GetKeyDown( KeyCode.Space ) )
{
    Vector3 rotateAround = transform.localToWorldMatrix.MultiplyPoint( Vector3.up );
    Matrix4x4 flip = CreateRotationMatrixAroundPoint( rotateAround, Vector3.right, 180f );
    Quaternion flipRot;
    ExtractRotationFromMatrix( flip, out flipRot );

    //transform.position = flip.MultiplyPoint( transform.position );
    //transform.rotation = flipRot * transform.rotation;
    
    transform.rotation = flipRot * transform.rotation;
    transform.position = flip.MultiplyPoint( transform.position );
}