current | The current position. |
target | The position we are trying to reach. |
currentVelocity | The current velocity, this value is modified by the function every time you call it. |
smoothTime | Approximately the time it will take to reach the target. A smaller value will reach the target faster. |
maxSpeed | Optionally allows you to clamp the maximum speed. |
deltaTime | The time since the last call to this function. By default Time.deltaTime. |
Gradually changes a value towards a desired goal over time.
The value is smoothed by some spring-damper like function, which will never overshoot. The function can be used to smooth any kind of value, positions, colors, scalars.
using UnityEngine;
public class Example : MonoBehaviour { // Smooth towards the height of the target
Transform target; float smoothTime = 0.3f; float yVelocity = 0.0f;
void Update() { float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime); transform.position = new Vector3(transform.position.x, newPosition, transform.position.z); } }