- Home /
damptime and deltatime in setfloat parameters
Please can somebody explain to me (cause I am not native english speaking person and i don't get that DAMP means, cause all dictionaries i found are pointin to words like "wet" and no others) what DAMPTIME MEANS ? And why is there Time.deltaTime
Should I call the setFloat on update or just once ? Following the previous question:
question part 1:
I am trying to use 2D BlendTree in mecanim, and I would like to fade the float speed from 0 to 1, it's accomplished by:
animator.SetFloat("speed", desiredSpeed, damptime, deltatime) but I still don't get what that mean, i'd understand that the damptime is time needed to finish the fade but why is the deltatime there? and how is it counted? if i put there
animator.SetFloat("speed", _speed, 0.1f, Time.deltaTime); then it's slow, for sure not 0.1 second, so I am not quite sure what parameters to put there to have 1/10 sec fade time
question part 2:
when I use:
animator.SetFloat("speed", _speed);
then it works, but does not blend smoothly
and when i use
animator.SetFloat("speed", 1f,1f,Time.deltaTime);
then it stops at speed 0.0196...
animator.SetFloat("speed", 1f,0.01f,Time.deltaTime); // 0.66
animator.SetFloat("speed", 1f,0.001f,Time.deltaTime); // 0.95
animator.SetFloat("speed", 1f,0.0001f,Time.deltaTime); // 0.99
so I am not quite sure
I am calling the SetFloat only once.
Answer by GameVortex · Jan 08, 2014 at 10:03 AM
SetFloat with the dampTime is not an automatic function that will change the value over time for you after you have called it once. dampTime is the time you want it to take to get to the target value (sort of). While the deltaTime is the current frames deltaTime which is the time value the function bases its calculations on. The current deltaTime will be multiplied by the dampTime to create the smoothing. So by calling the function once the float value will be smoothed based on one calculation of dampTime by the amount of deltaTime.
Basically it does something like this (approximate):
currentValue = Mathf.Lerp(currentValue, targetValue, deltaTime * dampTime);
This is just a linear interpolation which moves one value towards another value by a small amount. So by calling this once the currentValue has moved a little towards your targetValue.
The solution here is that you will have to call SetFloat every update to create the smoothing effect. Which makes the currentValue move towards the targetValue every frame.
i actually do update it every frame now, i just wasn't sure if it's the efficient way
thank you very much sir!!!
No problem. =)
You are not supposed to close questions when they are answered though. You should mark the question specifically answered by pressing the button next to the answer that best worked for you.
Thanks for this explanation. The Unity documentation is a tad short on this function.
I do use a slightly extended version of this code in my current project:
float dampTime = $$anonymous$$athf.Clamp(input.horizontal.since + 0.1f - Time.time, 0f, 0.1f);
animator.SetFloat("Speed", $$anonymous$$athf.Abs(input.horizontal.value), dampTime, Time.deltaTime);
This way the given value is reached after 0.1 seconds. Without this the animator value will crawl nearer to the given value every frame, but will never reach it.
We're using the two lines above to feed the speed input to the animator. Inside it we use a blendtree for walking animations. These animations have events for the footfalls which cause the sound to be played.
Now when the value is very small the character won't move, but the animations in the blendtree still run, causing the footfall sound to be played even though the character is not moving. Which is what happened with the original code seen above.
Now the value of "Speed" actually is 0 after 0.1 seconds and no footfall sounds are played.
Answer by Stuart Harrison · Oct 23, 2015 at 03:28 PM
For what it's worth, for reference to @komodor, "damp" in this sense is a contraction (shortening) of "dampen", in the second sense of the word here: http://www.thefreedictionary.com/dampen
"2. To deaden, restrain, or depress"
It's a word you might use to describe the job done by the suspension system on your car - that of smoothing out rough terrain (rapid changes in vertical position).
Hope that helps!
Answer by unityBerserker · Oct 29, 2018 at 12:57 PM
Signature of SetFloat():
public void SetFloat(string name, // name of parameter
float value, //value of parameter we want to reach
float dampTime,
float deltaTime); //how often we sample
To understand dampTime you must see graph below.
In this example I set dampTime to 1s.
So after 1s SetFloat() return 63% of value.
After 2s SetFloat() return 86% of value. ect.
If I set dampTime to 20s.
Then after 20s SetFloat() return 63% of value.
And After 40s SetFloat() return 86% of value. ect.
This script add to gameObject with Animator and change name of state in script (to name of state in your Animator)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetFloatWithDampTest : MonoBehaviour
{
[SerializeField]AnimationCurve ac;
Animator a;
string paramName = "ZZZ";
//parameter value we want to reach
int value = 100;
void Start ()
{
a = GetComponent<Animator> ();
ac.AddKey (new Keyframe (0, 0));
}
// I use FixedUpdate to get readable graph of how value change
// Use Update() or FixedUpdate() - depends how you use animations
void FixedUpdate ()
{
a.SetFloat (name: paramName ,
value: value,
//after this time we get 63% of value, after 2xdampTime we get 86% of value ect. - check graph
dampTime: 1f,
// how often we sample
deltaTime: Time.deltaTime);
AddKeysAndLogInfo ();
}
void AddKeysAndLogInfo ()
{
float percentage = a.GetFloat (paramName ) / value * 100;
percentage = (float)System.Math.Round (percentage, 2);
if (a.GetFloat (paramName ) != 0) {
ac.AddKey (new Keyframe (Time.fixedTime, a.GetFloat (paramName )));
Debug.Log (percentage + "% : " + Time.time + "s");
}
// I pause aplication when value is appriximately 99% because SetFloat() have problem with reaching 100% of value
if (percentage >= 99) {
Debug.Break ();
}
}
}
Your answer
Follow this Question
Related Questions
Is it possible to tween with mecanim like with iTween, GoKit...? 0 Answers
SetFloat Shader Value question/issue 0 Answers
Battlerite-lookalike walk animations 0 Answers
Mecanim Character Barrel Roll 0 Answers