In my project I don’t use the gravity of the physics engine (not anymore) I use my own vector gravity, which changes it’s direction if the player touches a gravity modifier. That’s cool, but I want the camera to rotate so thing’s won’t look upside down when the gravity changes.
Now for gravity change I just check if my player touches a gravity modifier and then sets the gravity vector:
The problem is the camera I need to somehow get the gravity vector and set the camera’s rotation so that the ground will always be bellow, the sky above etc. I tried to get the rotation of the gravity modifier and set the camera’s rotation according to that, but didn’t really succeed…the rotation was somehow random and didn’t really work…it was really strange.
My camera object looks like this:
I’m trying to rotate the GravityAlign object. Can you help me out with this?
Ah, got it working. For those who wonder how I did it. First I had my object reconstructed, because the structure from the post above was awful. I now have a base, that lerps to the object’s position and changes it’s rotation. I couldn’t think of a way to get a rotation from the gravity vector, so I just took all of the gravity modifiers, turned them upside down so the green axis would point to the ground and rewrote the gravity setting line so gravityVector = -col.transform.up;
Then I made it send the information about the local rotation of the modifier to the camera object:
camBaseObject.GetComponent(“CameraControl”).correctRotation = col.transform.localRotation;
and then in the camera object:
transform.localRotation = Quaternion.Slerp(transform.localRotation, correctRotation, Time.deltaTime*5);
Quaternion.LookRotation for this purpose, it allows you to specify the Up vector, which in your case would be (-1)*gravity.
Also you can actually change the gravity constant for all objects by assigning a Vector3 to the static variable Physics.gravity. So you don’t really need to use your own gravity system if your gravity modifiers change the gravity for all of your objects.