Changing gravity direction of player, and view.
I tried doing this thinking it shouldn't be too hard, but man am I wrong. Or maybe I am just bad.
First problem, I can't use any rigidbody base rotation (add torque, moverotation, rotation, etc), reason being I am using a capsule collider for my player, and physics is making it fall to its sides, and when colliding with walls, it just makes my player spin slowly for some reason, so I froze the rotation.
The changing gravity direction part is not the problem, but the problem is rotating SMOOTHLY to the corresponding direction. Basically my player can select any wall, ceiling, or floor they point at, and make that the ground floor for them, causing a change in gravity direction, then the player rotates where their bottom is touching the new ground floor.
I am not exactly sure what to do with this rotation. I know I need to use Quaternion.LookRotation, and I know the up direction is the normal of the new floor, but I don't know what forward direction it should be. Note that the player can be looking at a wall on an angle, then click on it, and the rotation happen, so the player's forward at the moment of click is not the same as vector3.forward, which is the most trouble some part.
I am just asking for a solution in general, not necessarily improving on top of the one I am doing, so any help will be much appreciated.
Answer by Eno-Khaon · Jan 24, 2016 at 08:46 AM
If you consider the way your character will likely be oriented when settled into a correct rotation at any given time, they will likely be standing upright, possibly leaning forward or backward. This means that your character's left and right will generally be guaranteed to be accurate or close enough.
But, your character's not already perfectly-enough aligned? Well, that's what axis-correction is good for.
First, take your character's current forward direction and up relative to the current gravity and take their cross product as your new right vector relative to the ground.
// C#
Vector3 newRight = Vector3.Cross(-gravity, transform.forward);
Now that you have a ground-aligned right vector, it's time to reconstruct your forward vector from it as your next baseline. This takes a cross product again:
Vector3 newForward = Vector3.Cross(newRight, -gravity);
With those, you now have your baseline orientation before applying any orientation modifiers (such as having your character lean into a turn, or looking around or up and down).
Answer by NGC6543 · Jan 24, 2016 at 08:47 AM
First of all you should use Quaternion.Lerp or Quaternion.LookRotation to make it being smoothly interpolated. And use transform.forward instead of Vector3.forward. transform.forward uses your player GameObject's local forward direction.
And the stability issue you asked... you can stabilize the GameObject in the LateUpdate() phase.
Void LateUpdate(){
transform.rotation = Quaternion.identity;
}
This code uprights a GameObject no matter how much torque is applied. But your case the GameObject does change their rotation, so you have to do some work to fit your need.
Hope this helps.
Your answer
Follow this Question
Related Questions
Changing gravity depending on character/camera rotation 0 Answers
CineMachine Freelook - X axis breaks when WorldUpOverride is sideways, any way to fix? 0 Answers
How to make physics for the string of a rising balloon 1 Answer
Smooth Walking Inside a Sphere 0 Answers
Adding Gravity to a game object to make a black hole sucking effect. 1 Answer