Unity中使用Lerp()函数实现缓动效果

    xiaoxiao2022-07-05  286

    在Unity里面Lerp函数可以实现缓动效果 using UnityEngine; using System.Collections;

    public class test : MonoBehaviour {

    private Vector3 newPos; void Start () { pos = transform.position; } void Update () { if(Input.GetKeyDown(KeyCode.A)) pos = new Vector3(-3,8,22); if(Input.GetKeyDown(KeyCode.D)) pos = new Vector3(3,8,22); transform.position = Vector3.Lerp(transform.position,pos,Time.deltaTime); }

    } 若Lerp(from(起点),to(终点),value),则 插值函数Lerp()的工作原理(公式): 返回值 = from+value*(to - from); 需要注意的是,这里的value取值范围为0到1。 上例代码中使 value=Time.deltaTime(每帧中的时间增量(一般来说是从0到1的一个动态数值)),这样当Time.deltaTime平滑增加时,通过插值函数的作用,就时物体的position平滑变动,物体就实现了缓动的效果。

    最新回复(0)