- Home /
Other
Smooth Rotation
My player model always turns to face a target in front of the camera; as you move your mouse, you move the camera, and the player then adjusts to match that target (think of movement in FPS shooters, just third person).
The issue is, when I move diagonally I want my player model to turn a set amount. The movement is based on local space, so if I turn the player object, the rotation causes the movement direction to change with it. What I did to partially solve my issue is getting the armature of the player and rotating that separately from the main body, after the main body adjusts to face the camera target.
Here is the code I use to make the player follow the mouse:
void PlayerFollowMouse()
{
Vector3 MouseScreenPosVector3 = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, lookDistance));
target = new Vector3(MouseScreenPosVector3.x, transform.position.y, MouseScreenPosVector3.z);
var lookRot = Quaternion.LookRotation(target - transform.position);
float extraRotate = playerController.DiagonalRotation(); // Get extra rotation value from player controller
transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, Time.deltaTime * turnSpeed);
playerBody.transform.Rotate(0.0f, 0.0f, extraRotate); // Rotate player model x amount to match movement direction
}
Now, the use of playerBody.transform.Rotate allows me to do what I want, only that it snaps the model to that angle. I'd like to rotate it smoothly to the desired angle, but I've tried using Quaternion.Slerp, and it just doesn't work. Are there other ways to rotate a character smoothly? Is there specific way to approach this situation that perhaps I haven't tried? Any and all help is appreciated.
Follow this Question
Related Questions
Bank Camera Based On Rotation Delta 1 Answer
Rotate camera around object smoothly 2 Answers
Smooth tilting rotation on slopes like cars? 1 Answer
Rotate Gameobject on Z axis only (.JS) 1 Answer
Curve player movement 1 Answer