Experiencing strange problems with Mathf.SmoothDamp function

So, here’s the code I"m using with Mathf.SmoothDamp:

currentAccuracy = Mathf.SmoothDamp(currentAccuracy,Accuracy,vel,RecoveryTime);

“vel” is a float that’s set to 0.0

Basically, it’s just setting your current accuracy to your accuracy over RecoveryTime. Or it’s supposed to do that.

RecoveryTime grows larger every time you fire, up to a maximum of 1 (so 1 second). I know that works because it stays under or equal to 1 in the inspector.

For some reason, that line of code only affects currentAccuracy if currentAccuracy is under 0. Then, it sets currentAccuracy up to 0 over RecoveryTime (I think it’s over RecoveryTime…but either way, it’s still behaving unexpectedly for me).
Why isn’t it setting currentAccuracy to Accuracy, and why is it only affecting currentAccuracy if currentAccuracy is under 0?

When I set SmoothTime to 1 instead of RecoveryTime, then it seems to work without requiring currentAccuracy to be under 0, but it doesn’t set it to Accuracy…it sets it slightly under it for some reason.

Now, when I was fiddling around with it, it seems to start spiking my FPS to 10 on every second frame and then letting it back up to 80 the next frame…

Can anyone tell me what I’m doing wrong?

Thanks!

2 Answers

2

Are you setting vel to 0 just once (or each time you fire,) then leaving it alone?

SmoothDamp uses vel as its memory of the previous speed. Each call, SmoothDamp adjusts vel, then , next call, uses vel as the “old speed.” If, say, it’s 0 each call, SmoothDamp never gets up to any speed. For fun, if you make vel public, you should see SmoothDamp changing it.

I was setting it just once at the very start of the script...I tried each time I fired, but it still reacted the same. In the Inspector, vel stays at 0 throughout the entire thing... Am I supposed to be doing something different with vel?

Retried it -- vel should be changing. If it somehow doesn't, then currectAcc should still be changing very slowly. Being slightly under the final value is normal -- it's supposed to slow down before the target. Umm...javascript...is anything accidentally an int?

If vel never changes, smoothDamp always thinks you're going from a complete stop, so it should take forever. The entire problem is that vel is always 0. For a test, try setting vel to 1, every frame. If it still says 0, that really narrows it down. You didn't already have a vel for each weapon? I can't think of how it would be any way besides having SmoothDamp and vel in the weapon script.

Right now, I use a For loop to go through each weapon once, calling Mathf.SmoothDamp on each one with the For's "i" value as the array number (you have 5 weapons). However, when I try to replace vel with (weapon class).vel, it says the error I mentioned above. Why is that? Is it not considered a float because it's part of the class..?

I'd do a quick test of the plumbing -- at start set equip.curWeap*.vel to 56.78 and print it back, that sort of thing. It will probably give you the same errors about gameObjects, but will narrow it down to the basics.* Seems like a lot that could go wrong with somewhere in that chain (but nothing wrong with the idea,) and javascript may very well give you an emtpy game object whenever it gets confused. At worst, you'll have a new Q about using JS classes (for someone who uses javascript.)

So simple yet so hard to discover…
Thank you very much! the above works.

Best change vel to zero continuesly if you are using smoothdamp