- Home /
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!
Answer by Owen-Reynolds · Sep 10, 2011 at 03:32 PM
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?
currentAccuracy was an integer and so was Accuracy, so I changed them to floats and it started to work, but vel wasn't changing and it took like, 10 seconds ins$$anonymous$$d of RecoveryTime (which is always anywhere from 0.0-1.0 via $$anonymous$$athf.$$anonymous$$in). I tried giving each weapon its own vel and using that, but it said it didn't come up with a definition for $$anonymous$$athf.SmoothDamp(System.Object,System.Object,System.Object,System.Object) Something like that. I guess for some reason, vel doesn't count as a float anymore - it counts as an object...? Because I put it in the class that holds each weapon's individual stats and referenced it to that. I don't really know what to do now...it seems like this function is a whole lot of work. $$anonymous$$aybe I should try making my own or something, heh...
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 $$anonymous$$athf.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..?
Answer by Zach Stefanakis · Mar 22, 2012 at 05:54 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612205458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Call a function dependent on frame of animation? 2 Answers
How do I tell another script to skip its routine for one frame? 1 Answer
How do you find Unity's frames/second? 4 Answers
Ensure that function only gets called once in a frame 2 Answers
How to change GUI.DrawTexture with OnMouseEnter function? 1 Answer