- Home /
How does Mathf.SmoothDamp's max speed work?
I want a to get a new target y position for a transform to move smoothly, but has a max move speed. Initially I thought I could just do float newYPosition = Mathf.SmoothDamp(transform.position.y, targetYPosition, ref currentVelocity, moveSmoothTime, moveSpeed);
as I know Mathf.MoveTowards() works similarly but without the smoothing. I thought the value that would come out of the method would be a new position that if applied to the transform, it wouldn't surpass the speed of "moveSpeed" but my transform is moving very slowly and definitely not reaching "moveSpeed". I know it's not working because when I set the "moveSmoothTime" to 0 (no smoothing), my transform's y does not snap to "targetYPosition" like it would if I didn't include moveSpeed (maxSpeed). Tried multiplying "moveSpeed" by Time.deltaTime and transform moves slower. Tried dividing by Time.deltaTime, it seemed to work but I know for sure the transform was moving at a faster speed than my "moveSpeed" variable. Any help would be much appreciated!
Answer by Xarbrough · Sep 19, 2019 at 08:43 AM
Maybe post your complete code and compare to the sample in the scripting docs. The smooth time variable is the approximate time in seconds it will take to reach the target, e.g. 3 seconds. You can additionally provide a maxSpeed variable of e.g. 1 which will clamp the speed to 1 unit per second. In such a case it may be possible that it takes much longer than 3 seconds to reach the target if it is far away. Also, if you input a smoothTime of 0.1f and a maxSpeed of 5000 you will most likely not reach maxSpeed and so on. You do not need to include any deltaTime calculations by yourself, since the function already uses deltaTime by default.
Try this example on a simple cube in the scene and experiment with the values to see how they affect the speed:
using UnityEngine;
public class Move : MonoBehaviour
{
public float targetHeight = 10f;
public float smoothTime = 3f;
public float maxSpeed = 100f;
float yVelocity = 0.0f;
void Start()
{
transform.position = Vector3.zero;
}
void Update()
{
float newPosition = Mathf.SmoothDamp(transform.position.y, targetHeight, ref yVelocity, smoothTime, maxSpeed);
transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
}
}
Ok, running this script in my project helped me realize what I was doing wrong, and it's a bit silly. $$anonymous$$essing around with smoothTime variable I noticed that if i'ts 0 it will for some reason follow very very slowly, but anything higher than 0 (ie 0.01) would work just fine and be snappier. Turns out that in my project I had smoothTime as a unserialized private variable so it defaulted to 0. When running it would move my object very slowly. editing this value revealed that the code was working properly all along and that my max speed variable was indeed working. I appreciate the time taken to help out, the example code really helped me isolate my problem, thank you!