0
\$\begingroup\$

Following the advice posted here, I made a camera that follows a player behind with:

    public Camera roomCam;
    public GameObject playerGO;
    private float camMotionSpeed = 5f;
    private float camRotationSpeed = 50f;

    void Start() {
        camOffset = new Vector3(0, 3, -8);  //x=sideways, y=up/down, z = forward/back
        roomCam.transform.position = playerGO.transform.position + (roomCam.transform.forward * camOffset.z) + (playerGO.transform.up * camOffset.y);
        roomCam.transform.LookAt(playerGO.transform.position);
    }

    private void UpdateCameraPosition() {
        
        //Move
        Vector3 newCamPosition = playerGO.transform.position + (playerGO.transform.forward * camOffset.z) + (playerGO.transform.up * camOffset.y);
        newCamPosition = Vector3.Slerp(roomCam.transform.position, newCamPosition, Time.smoothDeltaTime * camMotionSpeed); //spherical lerp smoothing
        roomCam.transform.position = newCamPosition;

        //Rotate
        Quaternion newCamRotation = Quaternion.LookRotation(playerGO.transform.position - roomCam.transform.position);
        newCamRotation = Quaternion.Slerp(roomCam.transform.rotation, newCamRotation, camRotationSpeed * Time.smoothDeltaTime); //spherical lerp smoothing
        roomCam.transform.rotation = newCamRotation;

    }

    private void LateUpdate() {
        UpdateCameraPosition();
    }

This works correctly in general, but the smoothing function which is just meant to make the camera slowly "catch up" in position and rotation to the user as they move around is quite jerky.

Smoothing specifically is done by the lines:

        newCamPosition = Vector3.Slerp(roomCam.transform.position, newCamPosition, Time.smoothDeltaTime * camMotionSpeed); //spherical lerp smoothing
        newCamRotation = Quaternion.Slerp(roomCam.transform.rotation, newCamRotation, camRotationSpeed * Time.smoothDeltaTime); //spherical lerp smoothing

Is there some better way of smoothing this that can be applied that will not create so much jerking or wobbly jello motion?

Thanks.

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered the built-in SmoothDamp functions? \$\endgroup\$ Commented Mar 24, 2022 at 8:32
  • \$\begingroup\$ Thanks. I wasn't aware of that. Something new I learned but it was not the issue. FYI, I did find the issue here: forum.unity.com/threads/camera-jitter-problem.115224 If you're following a physics based object you need to use FixedUpdate() not LateUpdate() \$\endgroup\$ Commented Mar 25, 2022 at 3:51
  • \$\begingroup\$ If you've solved your problem, be sure to post your solution as an answer below. You can also enable Rigidbody interpolation to smooth the physics object's movement between FixedUpdates \$\endgroup\$ Commented Mar 25, 2022 at 11:13

1 Answer 1

1
\$\begingroup\$

Solution was described here: forum.unity.com/threads/camera-jitter-problem.115224

If you're following a physics based object you need to run UpdateCameraPosition() on FixedUpdate() not LateUpdate().

\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to mark this answer as Accepted if it worked for you. \$\endgroup\$ Commented Oct 14, 2022 at 23:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.