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<Renderer>().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来生成的,大家可以多多尝试

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容