UnityEngine.AnimationCurve的使用

UnityEngine.AnimationCurve

  • 创建:创建脚本挂载在物体上,定义public AnimationCurve curve;然后在Inspector面板就可以看到多了一个Curve,点开后可进行编辑
  • 简单使用:
public void Update()
{
transform.position = new Vector3(Time.time, anim.Evaluate(Time.time), 0);
}
  • 再做一个简单的案例:
    利用曲线控制小球的运动轨迹和颜色
using UnityEngine;
public class curvetest : MonoBehaviour
{
  public AnimationCurve curve;
  public Color oldColor;
  public Color newColor;
  public float timeScale = 1;
  bool startAnimation = false;
  float startTime = 0;
  float currentTime = 0;
  Material mat;
  void Start()
  {
    mat = transform.GetComponent().material;
    Invoke("ChangeState", 2f);
  }
  void ChangeState()
  {
    startTime = Time.time;
    startAnimation = true;
  }
  float CurrentTime
  {
    get
    {
      if (startAnimation)
      {
        currentTime = ((Time.time - startTime) * timeScale) % 1;
      }
      else
        currentTime = 0;
      return currentTime;
    }
  }
  void Update()
  {

    if (startAnimation)
      UpdateAnimation();
  }
  void UpdateAnimation()
  {
    transform.localPosition = new Vector3(0, curve.Evaluate(CurrentTime), 0);
    mat.SetColor("_Color", Color.Lerp(oldColor, newColor, curve.Evaluate(CurrentTime)));    
  }
}

看完这段代码,应该已经会使用AnimationCurve了,基于Curve我们还可以做更多的事情,许多的面片特效的插件就是通过用Curve来控制面片的scale和position来生成的,大家可以多多尝试

你可能感兴趣的:(UnityEngine.AnimationCurve的使用)