What is the purpose of the deltaTime parameter in animator.SetFloat

Why does animator.setFloat take deltatime as an input, and thusly have it available as a parameter in the function? This is a value that can easily be accessed from anywhere with Time.deltaTime, right?

This seems redundant and unnecessary.

1 Answer

1

You don’t have to provide Time.deltaTime to SetFloat - if you want to just set a float value instantly, call

animator.SetFloat("speed", _speed);

However, there is an overload of SetFloat that allows you to smoothly change a float over time (basically a lerp), whose function signature is:

void SetFloat(int id, float value, float dampTime, float deltaTime); 

This changes a float to value over dampTime amount of time, where deltaTime is the amount of time that’s elapsed since the last time the function was called. It doesn’t automatically retrieve Time.deltaTime for you because if you were calling this method from FixedUpdate, you’d want to use, say, Time.fixedDeltaTime instead, or you might want to calculate a more elaborate timestep function.

Below is post with explanation how work this version of SetFloat() void SetFloat(int id, float value, float dampTime, float deltaTime); https://answers.unity.com/questions/611667/damptime-and-deltatime-in-setfloat-parameters.html?childToView=1566804#answer-1566804