在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)進行緩動了。