- Home /
How do I both align my character with the normals of a slope AND rotate him in the direction of movement?
How do I properly align the player with the slope below him correctly AND rotate towards his movement direction? My player walks and moves based on Physics and adjusts based on the camera's view. However, I am having issues with rotating the character to face his direction of movement and rotating to align with the slope. Here is my code I have compiled with research into the topic online:
void RotateCharacter()
{
Ray ray = new Ray(playerCF.position + new Vector3(0.0f, 0.1f, 0.0f), -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundLayer))
{
leanDirection = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), 0.1f);
transform.rotation = Quaternion.Slerp(transform.rotation, leanDirection, turnSpeed);
}
if (axis > 0.0f)
{
targetRotationForDirection = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(movement), 0.5f);
//Quaternion originalRot = transform.rotation;
//transform.rotation = originalRot * Quaternion.AngleAxis(targetRotationForDirection.y, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotationForDirection, turnSpeed);
}
}
The Vector3 movement is what is passed into the AddForce() function to move the capsule collider. Hope I can get some help. Thank you!
Your answer
Follow this Question
Related Questions
Wrong normals on collision 1 Answer
Hockey Game collider objects going through objects 0 Answers
Rigidbody doesn't rotate after collision 3 Answers
How to prevent a thrown object from rotating but not effect the physics of its collision? 0 Answers
How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer