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.