0
点赞
收藏
分享

微信扫一扫

Unity(九):Lerp线性插值、SmoothDamp


Mathf.Lerp

/**
* Mathf.Lerp 函数接受 3 个 float (from, to, 插值)
* 参数:
* 一个 float 参数表示要进行插值的起始值
* 另一个 float 参数表示要进行插值的结束值
* 最后一个 float 参数表示要进行插值的距离。
* 在此示例中:
* 插值为 0.5,表示 50%。
* 如果为 0,则函数将返回“from”值;
* 如果为 1,则函数将返回“to”值。
*/
float result1 = Mathf.Lerp (3f, 5f, 0.5f);
Debug.Log(result1); // 4

Vector3.Lerp

Unity(九):Lerp线性插值、SmoothDamp_ide

// 定位
Vector3 from = new Vector3 (1f, 2f, 3f);
Vector3 to = new Vector3 (5f, 6f, 7f);
Instantiate(cubePrefab, from, Quaternion.identity);
Instantiate(cubePrefab, to, Quaternion.identity);
Vector3 result2 = Vector3.Lerp (from, to, 0.75f);
Instantiate(cubePrefab, result2, Quaternion.identity);
Debug.Log(result2); // (4.0, 5.0, 6.0)

Lerp、SmoothDamp

Unity(九):Lerp线性插值、SmoothDamp_ide_02

public Light light;
public GameObject capsule;

private void Update()
{
// 随帧率改变灯光强度 一直到 8f
light.intensity = Mathf.Lerp(light.intensity, 100f, 0.5f);

// 随时间改变灯光强度 趋近到 8f
light.intensity = Mathf.Lerp(light.intensity, 100f, 0.5f * Time.deltaTime);

capsule.transform.position = Vector3.Lerp(capsule.transform.position, new Vector3(5f, 6f, 7f), 0.3f * Time.deltaTime);

// SmoothDamp
Vector3 velocity = Vector3.zero;
capsule.transform.position = Vector3.SmoothDamp(capsule.transform.position, new Vector3(5f, 6f, 7f), ref velocity, 0.3f);
}


举报

相关推荐

0 条评论