public static float SmoothDamp (float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed= Mathf.Infinity, float deltaTime= Time.deltaTime);

参数

current当前位置。
target尝试达到的目标。
currentVelocity当前速度,此值由函数在每次调用时进行修改。
smoothTime达到目标所需的近似时间。值越小,达到目标的速度越快。
maxSpeed可以选择允许限制最大速度。
deltaTime自上次调用此函数以来的时间。默认情况下为 Time.deltaTime。

描述

随时间推移将一个值逐渐改变为所需目标。

值通过某个类似于弹簧-阻尼的函数(它从不超过目标)进行平滑。 该函数可以用于平滑任何类型的值、位置、颜色、标量。

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); } }