Modifying SmoothDamp velocity?

So I have a Script, where an object is moved via SmoothDamp.
In another part of it, I modify the Velocity parameter, to simulate some kind of recoil.
But after modifying the velocity the object starts shaking off and bugging around.
I thought SmoothDamp modifies the Velocity to get a smooth transition, and therefore would smoothly catch the recoil and bring the object back towards the target.
Does Anyone know a Way to simulate this recoil, and can explain what exactly SmoothDamp does?

SmoothDamp is a static function and thus can’t store its own state. making the caller store the state for it, which is why Velocity has to be passed in by reference.

That said it has the assumption that nothing else is actually changing its velocity. it calculates its new velocity based on several factors: the current velocity, distance from current to target, smooth time, and max speed. changing the velocity outside the function without understanding the math behind it would undoubtedly cause issues with its calculations.

SmoothDamp is basically Clamped motion and would never try to give a point or velocity that could make it end up farther from its target.

instead of smoothDamp you could try and hold a velocity and perform a

 velocity = Vector3.MoveTowards(velocity, (homeposition- transform.position) * springStrength, Time.deltaTime * maxAcceleration);

when you start the recoil you’d set the velocity = recoilDirection * recoilStrength.
and the MoveTowards would spring it back based on how strong maxAcceleration and springStrength is and slow down the closer it got to the homeposition.

a high maxAccleration will nullify the recoil while low would give strong recoil and can overshoot homeposition.
a high springStrength (over 1) can have velocity overshoot the homeposition while less than 1 will dampen the resetting and exaggerate the recoil.

1 Like