Changing gravity axis while maintaining look direction

To begin with, I’ve been using the [gravityfpswalker][1] script. I then have another script that simply changes the gravity vector, which rotates the player. The problem I am having, is that in some situations, the direction the player is looking before rotating to a different axis is getting completely changed. This was happening using this:

Vector3 gravityForward = Vector3.Cross(Gravity, transform.right);
Quaternion targetRotation = Quaternion.LookRotation(gravityForward, -Gravity);
rigidbody.rotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, RotationRate);

Method in the original gravityfpswalker script. It had problems with rotating to the left and right axis, until I modified the gravityforward to not rely on transform.right as that would cause a zero look rotation with gravity being ±9.81 on the x-axis. But this still caused problems with the camera not rotating properly after changing the axis. It does from certain axis but not from others.

Right now I’m using

 if (Gravity.y == 9.81f)
    		{
    			Vector3 eulerAngles = transform.rotation.eulerAngles;
    			eulerAngles = new Vector3(0,0,-180);
    			transform.rotation =  Quaternion.Euler(eulerAngles);
}

Using similar code to rotate to all of the different axes I tried using rigidbody.rotations as well, but they have the same issue. I have all the axes set up properly so that it rotates the character and sets the gravity to the correct value.

What I want to happen is the camera to continue to look straight ahead relative to the new “ground” axis after changing the gravity. It does work in some situations in both rotation methods I have tried, but in a lot of situations the camera rotates around in seemingly weird directions.

As an example, using the 2nd script above, when coming from either gravity.x =±9.81 to the gravity.y = 9.81f, the camera rotates properly and points in the direction based on where I was looking before I changed axis. However when coming from gravity.z=±9.81 the camera does a complete 180 degree rotation. There are of course other problems, but I don’t completely understand why its not working.

Any help or ideas of stuff to try would be appreciated. If I can’t figure out how to do it kind-of simply, I guess I’ll just have to set up the rotation based on each axis I’m coming from and each I’m going to.
[1]: http://wiki.unity3d.com/index.php/GravityFPSWalker

just wanted to say I ended up doing it the long way.
ie:

if  (oldGravity != newGravity)
{
				
	if (oldGravity == Vector3.down * 9.81f) 
	{
		if (newGravity == Vector3.left *9.81f)
		{	
		rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.forward, -90);
		}	
		if (newGravity == Vector3.right *9.81f)
		{
			rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.forward, 90);
		}
		if (newGravity == Vector3.up *9.81f)
		{
			rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.forward, 180);
		}
		if (newGravity == Vector3.forward *9.81f)
		{
			rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.right, -90);
		}
		if (newGravity == Vector3.back *9.81f)
    	{
    		rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.right, 90);
        }
    }
}

for each of the different gravity vectors. This keeps the mouse looking in the same direction relative to where it was before.

The next thing I am going to do is use as an example:

rotlerp = Mathf.Lerp(0, -90, RotationRate);
currentRotation += rotlerp;
rigidbody.transform.RotateAround (rigidbody.transform.localPosition, Vector3.forward, rotlerp);

to smooth out the instantaneous change from rotate around. I tested it out, and it works, with some checks using currentRotation to make sure it doesn’t over rotate, and actually completes the rotation.

This problem has reminded me that sometimes taking the easier to understand but longer to code method is generally easier for me to do.