Unity的Lerp函数实现缓动

在Unity里面Lerp函数可以实现缓动效果

下面例子实现点光源的移动

在场景中创建好一个平面,一个点光源,我在这里随便放了一个模型。

然后新建c#脚本,代码如下:

using UnityEngine;
using System.Collections;

public class Lerp : MonoBehaviour {
    
    public Vector3 newPos;
    // Use this for initialization
    void Start () {
        newPos = transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.Q))
            newPos = new Vector3(-3,8,22);
        if(Input.GetKeyDown(KeyCode.E))
            newPos = new Vector3(3,8,22);
        
        transform.position = Vector3.Lerp(transform.position,newPos,Time.deltaTime);
    }
}

  然后将脚本拖动到点光上面,按下键盘Q和E键就可以看到效果了。

上面是用Vector3的Lerp函数进行缓动的。里面的参数是(Vector3 from,Vector3 to,float time);

比如我们想改变light的颜色或者强度intensity,那么参数是2个浮点数,我们就可以用Mathf.Lerp(float from,float to,float time)进行缓动了。

原文地址:https://www.cnblogs.com/louissong/p/3204447.html