Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited title
Link
mike
  • 501
  • 8
  • 22

Smoother method for making How to make camera smoothly follow behind player in Unity 3D?

Source Link
mike
  • 501
  • 8
  • 22

Smoother method for making camera follow behind player in Unity 3D?

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.