Search Issue Tracker
Won't Fix
Votes
0
Found in
2019.4
2020.3
2020.3.14f1
2021.1
2021.2
2022.1
Issue ID
1353189
Regression
No
SmoothDamp function does not reach it's target smoothly when smoothTime value is changed before the smoothing ends
Steps to reproduce:
1. Open users attached project
2. Open Smoothing Issue/Press Play scene
3. Enter the Play Mode
4. Observe Cubes movement in the Game View
Expected result: Cube smoothly stops on the left side
Actual result: Cube comes to a solid stop on the left side
Reproducible with: 2019.4.29f1, 2020.3.16f1, 2021.1.17f1, 2021.2.0b7, 2022.1.0a5
Note: Reproducible with both Mathf.SmoothDamp and Vector3.SmoothDamp
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- "Sub Mesh Mask" field in VFX Graph has no padding
- Source Generation with GeneratePropertyBag fails when using reserved keywords as variable names with @ prefix
- Prismatic ArticulationBody moves in opposite direction when using targetVelocity
- Shader Graph skybox material is rendered inccorrectly in builds when "Allow Material Override" or "Cast Shadows" are enabled and the "Deferred" Rendering Path is used
- Object motion is affected by incorrectly added rotation motion when importing animations
Resolution Note:
We understand that SmoothDamp doesn't work in that particular case. But since there is a lot of other projects relying on this method to works, changing this behavior could be problematic for those projects.
Unity SmoothDamp method is based on the book Game Programming Gems 4 Chapter 1.10 which has the same behavior and problem as Unity's SmoothDamp.
The best solution we can provide is to write your own version of smooth damp for your project if the one provided by Mathf doesn't work as expected for you.
You can use this code as an example:
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
// Based on Game Programming Gems 4 Chapter 1.10
smoothTime = Mathf.Max(0.0001F, smoothTime);
float omega = 2F / smoothTime;
float x = omega * deltaTime;
float exp = 1F / (1F + x + 0.48F * x * x + 0.235F * x * x * x);
float change = current - target;
float originalTo = target;
// Clamp maximum speed
float maxChange = maxSpeed * smoothTime;
change = Mathf.Clamp(change, -maxChange, maxChange);
target = current - change;
float temp = (currentVelocity + omega * change) * deltaTime;
currentVelocity = (currentVelocity - omega * temp) * exp;
float output = target + (change + temp) * exp;
return output;
}