[Unity tips] How to use AnimationCurve curve in Unity

Sometimes in addition to directly using variables to achieve some of the effects we want, we can also use curves directly and intuitively.

Create a script as follows, mount it on a 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;
        }
    }
}

Modify the curve, and you can see that the Cube zooms in and out smoothly with the curve.
insert image description here

Guess you like

Origin blog.csdn.net/qq_30163099/article/details/125414832