- Home /
SmoothDamp smoothTime ignored!
Hi :)
SmoothDamp.smoothTime is ineffective at controlling the duration of SmoothDamp. Has anyone had success using it?
In the following script SmoothDamp is set to reach it's target in 1 second...it's takes ~2.4 seconds. I know smoothTime is approximate but this amount of deviation makes SmoothDamp useless for a lot of applications.
Chuck this script on an object and and hit play. On the script toggle the 'StartSmoothDamp' bool and the SmoothDamp will run until it's within the defined threshold of the target value at which time it will log out how long the SmoothDamp took.
using UnityEngine;
using System.Collections;
public class SmoothDamp : MonoBehaviour
{
[Range(0f,1f)]
public float Current = 0f;
private float target = 1f;
public float Velocity;
public float Duration = 1f;
private float LastTime = 0f;
public float Threshold = 0.05f;
public bool StartSmoothDamp = false;
void Update ()
{
if (StartSmoothDamp) {
Current = Mathf.SmoothDamp (Current, target, ref Velocity, Duration);
if (Mathf.Abs (Current - target) < Threshold) {
Debug.Log ("Duration: " + (Time.time - LastTime));
StartSmoothDamp = false;
}
} else {
Current = 0f;
Velocity = 0f;
LastTime = Time.time;
}
}
}
Thanks for reading :)
Did you find a solution? I'm having exactly the same problem, it's not working as expected
Answer by Zorkman · Nov 17, 2015 at 08:02 AM
Hi!
There might be some kind of serialization conflicts since you are initiating the variables during compile time and also having them public so they become serialized. I guess that you sometime entered the 2.4s in the editor and now it disregards the Duration = 1f
you wrote in your class. I've had a similar problem a long time ago.
Also it seems that you are forcing the Current variable to stay between 0 and 1 using the Range()
attribute. This seems like a very weird idea since the Current variable is not something that should be tweaked (and actually not public for any other use than so you can debug it). Instead if you want to limit the range of variables (which are not meant to be tweaked) I would use Mathf.Clamp()
or Mathf.Clamp01
.
TIP!
Instead of having a public bool that enables/disables your smoothdamp, make use of unity's enabled property and use the OnDisable/OnEnable functions to reset your variables. Here's a list of all messages a MonoBehaviour uses: http://docs.unity3d.com/ScriptReference/MonoBehaviour.html
Hi Zorkman :)
Thanks for your reply. This scripts sole purpose was to test SmoothDamp.
I'm pretty sure there are no serialization conflicts.
I simply used the Range() attribute so it was easy to visualise the value of Current.
Thanks for the tip. I used a bool so I could repeatedly set it while running in the editor to test different durations.
Np :) Did you fix the problem? Another thing could be that maybe your Time.TimeScale is not 1.
Your answer
Follow this Question
Related Questions
Time.deltaTime not working correctly? 1 Answer
Flashlight Cooldown and Timer 1 Answer
How does DeltaTime handle game logic ? 1 Answer
When to use Time.deltaTime? 3 Answers
Mathf.Lerp happens instantly 2 Answers