Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mathf.SmoothDamp not calculating as expected

Discussion in 'Scripting' started by mrwilliams, Dec 31, 2015.

  1. mrwilliams

    mrwilliams

    Joined:
    Sep 23, 2015
    Posts:
    13
    Hi,
    It appears that I have come unstuck with the infamous unity scripting documentation.

    Either this is a bug, or I am really not understanding the documentation:
    http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html

    If I call this method with the following:

    float newValue = Mathf.SmoothDamp(0f, 1f, ref yVelocity, 2.5f);

    Variable newValue is assigned a value of 8+.

    What is throwing me here is the documentation in all its ambiguity, claiming the 4th param to be of type float and representing time. If 0.3f represents 300 milliseconds, 2.5f should equal 2500 milliseconds correct?

    Any help would be much appreciated.

    Cheers
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Usually the result should certainly be within the current value (0f) and the target value (1f). The time is used to adjust the velocity, such that the target value is reached within approximately this amount of time. The velocity itself is also important. Which value are you using for the velocity?
    Usually, if you want to start smooth, you should have an initial velocity of 0 and then let SmoothDamp take care of it.
     
  3. mrwilliams

    mrwilliams

    Joined:
    Sep 23, 2015
    Posts:
    13
    Thank you for your quick reply Dantus - much appreciated.

    I restarted and applied a clamp to velocity before the method call of 0f. I was getting a leak in there only some of the time. That fixes that, but time parameter still appears to be causing me issues.

    In my head (logically speaking), I would need to reduce the target time each time the update is accessed, else the smooth variable will never reach the target?

    I am using a stopwatch between the updates and subtracting the elapsed time from the target time in order to bring that in, but the result is always that the smoothing reaches the original target in only a few hundred milliseconds. Any thoughts on that?

    Cheers
     
  4. mrwilliams

    mrwilliams

    Joined:
    Sep 23, 2015
    Posts:
    13
    Dantus, this little bit on the end of your reply was the issue. I needed to store the ref out velocity and submit it each time to the method call as an update. Champion - thank you so much.

    How do I mark your answer as correct? I don't see an option to do that?

    EDIT: For anyone else reading this post, It should also be noted that TIME is not a constant when using this method. You need to subtract the elapsed time, each time you call the method until you reach your target time.
     
    Last edited: Dec 31, 2015