【Unity小技巧】如何在Untiy中使用AnimationCurve曲线

有些时候我们除了可以直接使用变量来达到一些我们想要的效果,还可以直接直观的使用曲线。

创建一个脚本如下,将其挂载在一个Cube上就可以

using System.Collections;
using UnityEngine;

public class UseAnimationCurve: MonoBehaviour
{
    
    
    //创建一个AnimationCurve
    [SerializeField]
    private AnimationCurve curve;
    //在多少秒内完成变化(总时间)
    [SerializeField]
    private float totalTime;

    private void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Space))
        {
    
    
            StartCoroutine(StartCurve());
        }
    }
    private IEnumerator StartCurve()
    {
    
    
        float time = 0;
        while(time <= totalTime)
        {
    
    
            float normalizeTime = (time / totalTime);
            time += Time.deltaTime;
            float curveValue = curve.Evaluate(normalizeTime);
            Debug.Log(curveValue);
            transform.localScale = new Vector3(curveValue, curveValue, curveValue);
            yield return null;
        }
    }
}

将曲线修改一下,即可看到Cube随着曲线而平滑的放大缩小。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_30163099/article/details/125414832