Hello. I’m working on a player controller for a first person shooter, based on a Rigidbody.
I wrote a script to handle mouse look, and I attached it to the Rigidbody. The first person camera is a child of the Rigidbody. The scripts holds a reference to the Transform of the camera object. Below there’s a picture showing the hierarchy, where Player holds the Rigidbody:
While rotating the Rigidbody by its up vector, the camera’s view jitters. Rotating the camera by its right vector doesn’t cause any jitter. Keep in mind that interpolation is enabled for the Rigidbody, as shown below:
Here’s an abstract of my scritp, where body
is the player’s Rigidbody and firstPersonCamera
is the camera’s Transform:
// Update is called once per frame.
void Update() {
UpdateMouseLook();
}
// It updates player's view, given mouse's input.
private void UpdateMouseLook() {
// Update mouseDelta, eventually damping it
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
mouseDelta = Vector2.SmoothDamp(mouseDelta, targetMouseDelta, ref mouseDeltaDamp, mouseSmoothness);
// Update pitch and roll
cameraPitch += (invertYAxis ? mouseDelta.y : -mouseDelta.y) * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90f, 90f);
cameraYaw += (invertXAxis ? -mouseDelta.x : mouseDelta.x) * mouseSensitivity;
cameraYaw %= 360f;
// Apply rotations
firstPersonCamera.localEulerAngles = Vector3.right * cameraPitch;
body.MoveRotation(Quaternion.Euler(0f, cameraYaw, 0f));
// Note: deltaTime isn't used here since mouseDelta is sufficient.
}
Previously, I moved the camera away from Player’s hierarchy. I handled their rotation separately, and that solved the jittering issue. The problem is I wish to keep the hierarchy simple and intuitive, as showed above, but I couldn’t find a solution for this jitter.
I tried to move the UpdateMouseLook
call in FixedUpdate
with very bad results. I also tried to move the last MoveRotation
call in FixedUpdate
, worsening the jitter. I also tried to build the project and running it from the executable, but the jittering was still there.
Any ideas on how to solve the jittering? Thanks in advance.