I’ve got a function that’s called in my update that used the camera rotation and GetAxis to move and rotate the player (long the Y axis). Now I want to to rotate X and Z so that the player is aligned with the slope of the floor beneath them. I’ve gotten as far as detecting the Floor’s Normal but I’m not sure how to translate it into a rotation given my current script. To be clear, nothing in the code is broken, I’m just not quite smart enough to figure out the next step. My function:
void MovementUpdate()
{
// Get Input for axis
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
// Calculate the forward vector
Vector3 camForward_Dir = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
Vector3 move = v * camForward_Dir + h * Camera.main.transform.right;
if (move.magnitude > 1f) move.Normalize();
// Calculate the rotation for the player
move = transform.InverseTransformDirection(move);
// Get Euler angles
float turnAmount = Mathf.Atan2(move.x, move.z);
//get floor normal/slope
RaycastHit hit;
Vector3 down = transform.TransformDirection(-Vector3.up) * 0.5f;
Physics.Raycast(transform.position, down, out hit);
if (hit.collider.tag != "Player")
{
Quaternion groundTilt = Quaternion.FromToRotation(Vector3.up, hit.normal);
Debug.Log(groundTilt);
}
transform.Rotate(0, turnAmount * RotationSpeed * Time.deltaTime, 0);
_moveDir.y -= Gravity * Time.deltaTime;
_characterController.Move(_moveDir * Time.deltaTime);
}