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