I have written this code for a camera to follow an object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetFollow : MonoBehaviour {
public GameObject FollowThis;
public float SmoothTime; //1f
Vector3 CurrenPos;
Vector3 TargetPos;
Vector3 vel = Vector3.one;
void FixedUpdate (){
CurrenPos = this.gameObject.transform.position;
TargetPos = FollowThis.gameObject.transform.position;
this.gameObject.transform.position=Vector3.SmoothDamp (CurrenPos, TargetPos, ref vel, SmoothTime);
}
}
When following an object, the camera horribly jitters. I have already tried putting this in Late and Fixed Update, and also set the target’s rigidbodies interpolation to interpolate, but jitter continues. Any idea what is going wrong?
I’ve set vel as Vector3.zero but nothing happened. Also setting SmothTime as 0f clamps but I do not want the camera to be always stuck on target. Moreover setting SmoothTime as 0f produces even more jitter. In fact, I’ve observed that less the value of SmoothTime, more is the jitter
If @Kurt-Dekker 's response is accurate, I’d investigate if something else is interfering, but still think late update is often preferred. Just check for whatever you think looks better.
Assuming you use source control or otherwise have good backups, open your scene and start disabling thing after thing until you figure out which one is causing you grief.
There’s only like four or five “things” required to show off the functionality of a SmoothDamp, and those are proven to work in the package I posted.
By deletion or disablement, slowly bring your scene down to the level of complexity of my scene, and when you find what fixes it, look there!
In the project’s Time settings, I changed the Fixed and Maximum allowed timestamp to 0.0166666 and the jittering reduced dramatically, but a little bit of shaking is still present.
I had similar problem with my camera follow script.
In my case camera follows player object with rigidbody component on it.
And jitter dissapeared when i choose in rigidbody component Interpolate setting “Interpolate”
Wrestled with this for many days… nothing helped except setting Fixed and Max Time to 0.01666* … my times were set at 0.2 and 0.3 … are those the Unity defaults? Why are they the defaults?
UPDATE: FWIW, I’ve tried release builds with Time.deltaTime and Time.smoothDeltaTime. smoothDelta is definitely ahh… smoother :). I see a lot of talk about just using deltaTime, but in my case at least, it is not smooth. I hope this helps someone.
Also, SmoothDamp uses deltaTime as its default, you have to pass in a smoothDeltaTime or, and again in my experience only, you’ll never get smooth scrolling.
No one knows and it doesn’t matter. It is expected to tweaked by the developer.
Also these settings are highly dependent on your framerate and/or on the framerate of your monitor. Fixing jerkyness is not a straight-forward thing and usually there is no global, good solution for everyone in Unity.