Is my (LateUpdate) camera + rigidbody causing jitter?

I’ve been updating an old camera script to use SmoothDamp instead of Lerp but something interesting came up. It seems there’s a bit of jittering involved in all of the scenes using this script. At first I thought I may have accidentally altered the rigidbody movement script so I made a parented test. I’m certain the camera script is causing the jitter. With a “smoothTime” value of 0f the camera is tight with no noticeable jitter, however, simply bumping this up to 0.1f creates an issue.

I’ll also provide the guts of the rigidbody movement code and the camera script. I have been using variations of these scripts for months and I’ve never had an issue so this definitely caught me by surprise. Could it be a timestep issue or is this entirely script-based?


My Rigidbody movement script:

	Vector3 refMove;
	Vector3 refV;
	Vector3 refH;

	void FixedUpdate(){
		refV = Camera.main.transform.TransformDirection(Vector3.forward);
		refH = new Vector3(refV.z, 0, -refV.x);
		refV.y = 0;

		refMove = (((Input.GetAxis("Horizontal") * speed) * refH.normalized) + 
            ((Input.GetAxis("Vertical") * speed) * refV.normalized));

		rg.velocity = AffectVelocity();
	}

	Vector3 newVelocity;

	public Vector3 AffectVelocity(){
		newVelocity = rg.velocity;

		if(Paused){ 
			newVelocity.x = 0f;
			newVelocity.z = 0f;
			return newVelocity; 
		}

		newVelocity.x = (refMove.x * speed);
		newVelocity.z = (refMove.z * speed);

		return newVelocity;
	}

My camera script:

    Vector3 camPosition;

	void LateUpdate () {
		camPosition.x = Mathf.Clamp(target.position.x + Xoffset, StopLeft_Map, StopRight_Map);
		camPosition.y = target.position.y + Yoffset;
		camPosition.z = target.position.z+ zoomDist;

		transform.position = Vector3.SmoothDamp(transform.position, camPosition, ref _velocity, 
            _smooth);

	}

The “target” is simply a reference to the object with the rigidbody on it.
Also, I have experimented with switching the camera transform code to FixedUpdate and Update. Nothing seems to be working.

Try calculating the camera’s position in the void Update() function of the Camera Script, rather than trying to calculate its next position at the same time as moving the camera.

I managed to get this fixed by changing two things. (Although this may not be the case for everyone I wanted to share my results).

In the camera script, Instead of setting the target to to the actual gameobject’s transform.position I set it to the Rigidbody.position


    camPosition.x = Mathf.Clamp(target.rigidbody.position.x + Xoffset, StopLeft_Map, StopRight_Map);
    camPosition.y = target.rigidbody.position.y + Yoffset;
    camPosition.z = ZPush + zoomDist;

Then, I changed the interpolation mode to extrapolate on the target rigidbody. Runs as smooth as it did before!

Again, this might not fix the issue for everybody but it was a great workaround for me.