- Home /
Align Player to Regional Up Direction, Without Altering Look Direction Dramatically
Alright, this is a complicated one, and I straight up apologize for any nose bleeds that result because of it.
I have a player can move in 360 degrees of rotation. I have platforms that have their own unique gravity, that is, the player is pulled to the ground when he enters the Spherical radius of one of these platforms.
I am not, and for obvious physics reasons cannot, use unity's built in gravity, but my faux gravity works fine.
What I cannot do, is align my player to the local gravity.
The MOST IMPORTANT THING HERE IS, when he is inside the gravitational sphere, he can only rotate left and right and up and down. No roll.
So the only thing I need to accomplish, is I need him to align his body to the ground without rotating to the direction of the normal.
I hope this is making sense.
I need the player to face up(no problem accomplishing that using hit.normal
), and then I need him to remain facing in the general direction he was facing.
Using hit.normal
always sets the transform.up
to the normal up, and transform.forward
to the normals forward. Which is always the same direction.
In laymens terms, if he is facing more east than any other direction when he enters the gravity field, I want him to face east and up. Not just up and whatever direction the hit normal is.
if(!gravity){
if(Input.GetKey(KeyManager.code.rollLeft)){roll += rollStep;}
else if(Input.GetKey(KeyManager.code.rollRight)){ roll += -rollStep;}
else roll = 0;
body.transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * lookSensitivity, Space.Self);
body.transform.Rotate(Vector3.right,-Input.GetAxis("Mouse Y") * lookSensitivity, Space.Self);
body.transform.Rotate(Vector3.forward, roll, Space.Self);
justSwitched = true;
}else{
// THIS IS THE PROBLEM AREA BELOW HERE.
if(justSwitched){
if(Physics.Raycast(body.transform.position, -gravitySource.up, out hit))
{
body.transform.up = hit.normal;
head.transform.up = hit.normal;
}
justSwitched = false;
}
float h = lookSensitivity * Input.GetAxis("Mouse X");
float v = lookSensitivity * Input.GetAxis("Mouse Y");
Vector3 bodyAxis = new Vector3(0, h, 0);
Vector3 headAxis = new Vector3(-v, 0, 0);
head.transform.Rotate(Vector3.Lerp(transform.rotation.eulerAngles, headAxis, 1), Space.Self);
body.transform.Rotate(Vector3.Lerp(transform.rotation.eulerAngles, bodyAxis, 1), Space.Self);
}