- Home /
Does SmoothDamp reach target?
This is rather a yes or no question but I would appreciate details. I'm wondering if Mathf.SmoothDamp
and Vector3.SmoothDamp
DO really reach their targets or just aproach to the target but never really reach it like Lerp
. Also, if this is the case, what tricks will one use to get it to reach the target?
Answer by hexagonius · Sep 17, 2017 at 02:55 PM
SmoothDamp is eventually reaching the target, but since the amount of adjustment is always smaller than what it takes to each it, or will only happen due to floating point precision.
You use it rather like a towrope, where it's not important to each any target.
Answer by pcbeard · Dec 30, 2019 at 12:47 AM
Here's how I'm currently using Vector2.SmoothDamp(), which may not be perfectly correct, but it does seem to do the job. I've written a generic smoother:
struct Smoothed<T> {
public T value;
public T velocity;
public delegate T smooth_t(T value, T targetValue, ref T velocity);
static public smooth_t smoother;
public void Smooth(T targetValue) {
value = smoother(value, targetValue, ref velocity);
}
}
Since it has a delegate, you have to fill it in for the type you're trying to smooth:
Smoothed<Vector2>.smoother = (Vector2 value, Vector2 targetValue, ref Vector2 velocity) => {
value = Vector2.SmoothDamp(value, targetValue, ref velocity, 0.2f, Mathf.Infinity, Time.fixedDeltaTime);
return velocity.sqrMagnitude <= 0.00001f ? targetValue : value;
};
I'm passing Time.fixedDeltaTime, since I'm smoothing values observed from FixedUpdate() like this:
public Vector2 position;
private Smoothed<Vector2> smoothPosition;
private void FixedUpdate() {
if (smoothPosition.value != position) {
smoothPosition.Smooth(position);
if (smoothPosition.value == position) {
Debug.Log("smoothPosition == position");
}
}
}
As you can see, when the smoother delegate sees the velocity go nearly to zero, it finishes by assigning the target value to the value, so my code can reliably do an exact comparison with the target value. A better, but maybe more expensive test would be to finish when the Euclidean distance between the current and target values is nearly 0. In practice, this seems to work fine.
Small correction to your statement. You do not have to use fixedDeltatime in FixedUpdate, deltaTime IS that value during FixedUpdate.
It's only when you need to access fixedDeltaTime anywhere else that you actually need to call it directly.
Your answer
Follow this Question
Related Questions
SmoothDamp or Lerp with Rootmotion to match a target position 0 Answers
Quaternions, Eulers, and SmoothDampAngle 0 Answers
SmoothDamp won't reach its target 1 Answer
Smooth damp the lowpass frequency in coroutine: value only changing in 1 frame 1 Answer
iOS game: Is it possible to target iphone 4 and ipad only? 1 Answer