Version: 2021.3

Mathf.SmoothStep

切换到手册
public static float SmoothStep (float from, float to, float t);

描述

minmax 之间进行插值,在限制处进行平滑。

此函数采用与 Lerp 相似的方式在 minmax 之间进行插值。 但是,插值会从起点逐渐加速,然后朝着终点减慢。 这可用于创建表现十分自然的动画、淡化和其他过渡。

using UnityEngine;

public class Example : MonoBehaviour { // Minimum and maximum values for the transition. float minimum = 10.0f; float maximum = 20.0f;

// Time taken for the transition. float duration = 5.0f;

float startTime;

void Start() { // Make a note of the time the script started. startTime = Time.time; }

void Update() { // Calculate the fraction of the total duration that has passed. float t = (Time.time - startTime) / duration; transform.position = new Vector3(Mathf.SmoothStep(minimum, maximum, t), 0, 0); } }