1. 程式人生 > >在兩個顏色之間來回變動(同理可延申至兩個位置之間)

在兩個顏色之間來回變動(同理可延申至兩個位置之間)

由於插值是 l=a*t+b(1-t),因此使用下面的這段程式碼

spriteRenderer.color = Color.Lerp (color01, color02, lerV); 

只要使得lerV(即t)在0到1之間來回變換,即可使得顏色在兩個顏色之間來回變換

 

public class Coin : MonoBehaviour {

public Color color01;
public Color color02;
public float smooth=1;
SpriteRenderer spriteRenderer;


// Use this for initialization
void Start () {
spriteRenderer = GetComponent<SpriteRenderer> ();
GameObject obj = GameObject.FindWithTag ("SceneInfoLoader");
if (obj)
playScene = obj.GetComponent<PlayScene> ();
}

// Update is called once per frame
void Update () {
float lerV=Mathf.PingPong(Time.time * smooth, 1);

spriteRenderer.color = Color.Lerp (color01, color02, lerV); 
}

}