简体   繁体   中英

How to lerp between two animation curves so it will play the first animation curve in the array and when it ends, it start playing the second?

In this case there are two animation curves in the array. One for fade in one for fade out. Currently I'm using the index 1 in the array but I want to lerp smooth between index 0 and index 1 so it will fade in and fade out nonstop smooth.

I want to play animation curve at index 0 when it finish to start play the second animation curve at index 1.

I tried to use a for loop before like this:

for (int i = 0; i < fadeIn.Length; i++)
{
    _renderer.material.SetFloat(shaderProperty, fadeIn[i].Evaluate(Mathf.InverseLerp(0, spawnEffectTime, timer)));
}

but it didn't change between the animation curves.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEffect : MonoBehaviour {
    public float spawnEffectTime = 2;
    public float pause = 1;
    public AnimationCurve[] fadeIn;
    public bool fadeOut = false;

    ParticleSystem ps;
    float timer = 0;
    Renderer _renderer;

    int shaderProperty;

    void Start ()
    {
        shaderProperty = Shader.PropertyToID("_cutoff");
        _renderer = GetComponent<Renderer>();
        ps = GetComponentInChildren <ParticleSystem>();

        var main = ps.main;
        main.duration = spawnEffectTime;

        ps.Play();
    }

    void Update()
    {
        FadeIn();
    }

    private void FadeOut()
    {
        
    }

    private void FadeIn()
    {
        if (timer < spawnEffectTime + pause)
        {
            timer += Time.deltaTime;
        }
        else
        {
            ps.Play();
            timer = 0;
        }
        _renderer.material.SetFloat(shaderProperty, fadeIn[1].Evaluate(Mathf.InverseLerp(0, spawnEffectTime, timer)));
    }
}

This is what I was looking for. Working great.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEffect : MonoBehaviour
{

    public float spawnEffectTime = 2;
    public float pause = 1;
    public AnimationCurve[] fadeIn;
    public bool fadeOut = false;

    ParticleSystem ps;
    float timer = 0;
    Renderer _renderer;

    int fadeIndex = 0;

    int shaderProperty;

    void Start()
    {
        shaderProperty = Shader.PropertyToID("_cutoff");
        _renderer = GetComponent<Renderer>();
        ps = GetComponentInChildren<ParticleSystem>();

        var main = ps.main;
        main.duration = spawnEffectTime;

        ps.Play();

    }

    void Update()
    {
        FadeIn();
    }

    private void FadeOut()
    {

    }

    private void FadeIn()
    {
        if (timer < spawnEffectTime + pause)
        {
            timer += Time.deltaTime;
        }
        else
        {
            ps.Play();
            timer = 0;

            if (fadeIndex == 0)
            {
                fadeIndex = 1;
            }
            else
            {
                fadeIndex = 0;
            }
        }

        _renderer.material.SetFloat(shaderProperty, fadeIn[fadeIndex].Evaluate(Mathf.InverseLerp(0, spawnEffectTime, timer)));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM