Calculating character lean parameter
Hi, I'm trying to implement leaning into my player character's animator but I'm having trouble wrapping my head around the rotation/math to implement it properly. The problem I'm having is that in the below code (last 3 lines specifically), the "Lean" parameter will function properly; but only when the characters y rotation is at or near 0. Otherwise "Lean" will stay at either -1 or 1. What I need is for the "Lean" to interpolate between -1 and 1 (-1 being a full left turn and 1 being a full right turn) when I begin a turn, always beginning and ending at 0. I should also add that plugging the analog stick into the "Lean" parameter is only a band-aid as I'm using an action adventure type controller, and I need it to react to the camera's position as well as my input. If there's a way to fix this code or an easier solution to the problem entirely, I would love to hear suggestions. Thank you!
void Turn()
{
// Forward direction: camPos -> playerPos
Vector3 cameraVector = new Vector3(transform.position.x - playerCamera.transform.position.x, 0.0f, transform.position.z - playerCamera.transform.position.z);
// Calculate the look direction of the player based on the input and the cameraVector
Vector3 playerLookDirection = Quaternion.LookRotation(cameraVector) * new Vector3(input.sideward, 0.0f, input.forward);
if (playerLookDirection != Vector3.zero)
{
// Calculate the specific player rotation to match the player look direction
Quaternion destRot = Quaternion.LookRotation(playerLookDirection);
// Perform smoothing from current player look direction to new look direction
transform.rotation = Quaternion.RotateTowards(transform.rotation, destRot, moveSettings.rotateVel * Time.deltaTime);
// Get the distance of the rotation
float destRotDistance = destRot.y - cameraVector.y;
// Get the rotation percentage
float rotPercent = destRotDistance / playerLookDirection.y;
// Plug the percentage into the animator
animator.SetFloat("Lean", destRotDistance);
}
}