I turned on the Input System & refactored my code in my project and noticed that when I move the mouse my character "lags" when moving the mouse slowly - as if the camera jumps from one position to another instead of moving smoothly.
So I want to make it so that moving the camera and the character is smooth but I have a problem.
I managed to make the camera moving smoothly on the X-axis, but I can't manage to make the character rotate smoothly on the Y-axis.
My current code responsible for rotating the camera and the character:
private void MovePlayerCamera()
{
// Get Y axis rotation and apply to capsule "player" (doesn't smooth rotation but i need to smooth it)
float yRotation = playerMouseInput.x * sensitivity;
transform.Rotate(0f, yRotation, 0f);
// Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
xRotation -= playerMouseInput.y * sensitivity;
xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);
// Setup a camera rotation goal and apply to player (this smoothing code works)
Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
}
I have already tried:
// Get Y axis rotation and apply to object (with this code player just rotates to right by itself)
float yRotation = playerMouseInput.x * sensitivity;
transform.Rotate(0f, Mathf.Lerp(transform.rotation.y, yRotation, cameraSmoothingStrength * Time.deltaTime), 0f);
But it doesn't work - The character rotates to the right side by itself :(
Any help is welcome and thanks in advance.
Try this:
private float targetYRotation;
private void MovePlayerCamera()
{
// Get Y axis rotation and store it as targetYRotation
float yRotation = playerMouseInput.x * sensitivity;
targetYRotation += yRotation;
// Smoothly interpolate the current rotation to the targetYRotation
Quaternion currentYRotation = transform.rotation;
Quaternion targetYRotationQuat = Quaternion.Euler(0f, targetYRotation, 0f);
transform.rotation = Quaternion.Slerp(currentYRotation, targetYRotationQuat, cameraSmoothingStrength * Time.deltaTime);
// Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
xRotation -= playerMouseInput.y * sensitivity;
xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);
// Setup a camera rotation goal and apply to player (this smoothing code works)
Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
}