简体   繁体   中英

Unity - how to lerp while a certain animation state is on?

Ok, so I have an animationnthe speed of which is controlled by user's tapping m meaning I cant just lerp for a set time but have to have it depend on if this is true:

cameraAnim.GetCurrentAnimatorStateInfo (0).IsName ("stillOpening")

By the end of this animation (no matter how long it took, fast or slow) I need this float in my material to have lerped to its final value:

skybox.SetFloat ("_Exponent1",Mathf.Lerp(skybox.GetFloat("_Exponent1"), topSkyBoxOpen, ratio));

Meaning it has to be equal to topSkyBoxOpen at the end of "stillOpening". I don't know how to coordinate the timing.

I have tried this in the Update():

void openSkyLerp()
    {
        float ratio = 0;
        float duration = 0.5f; // this is the one that will control how long it takes
        // value is in second 
        float multiplier = 1 / duration;

        while (cameraAnim.GetCurrentAnimatorStateInfo (0).IsName ("stillOpening")) {
            ratio += Time.deltaTime * multiplier;
            skybox.SetFloat ("_Exponent1",Mathf.Lerp(skybox.GetFloat("_Exponent1"), topSkyBoxOpen, ratio));

        }
    }

But nothing happens at all - I read this might be because its trying to have it all lerp in 1 frame. Is this possible? How can I lerp WHILE an animation is playing regardless of its speed?

Meaning it has to be equal to topSkyBoxOpen at the end of "stillOpening". I don't know how to coordinate the timing.

The problem is that you are not even waiting for a frame. So, even if your equation is correct, all those cannot happen smoothly in a single frame. You wait for a frame with yield return null; and that requires coroutine.

Answered a very similar but not the-same question few hours ago.

If the stillOpening variable is the destination value, get the current _Exponent1 value before going into the while loop. Have a counter variable that increments each frame while counter is less than topSkyBoxOpen . You can then use Mathf.Lerp(currentVal, topSkyBoxOpen, counter / duration); in the SetFloat function.

bool running = false;

void openSkyLerp()
{
    if (running)
    {
        return;
    }
    running = true;
    StartCoroutine(OpenSky(5));
}

IEnumerator OpenSky(float duration)
{
    float currentVal = skybox.GetFloat("_Exponent1");
    float counter = 0;
    while (counter < topSkyBoxOpen)
    {
        //Exit if not still opening
        if (!cameraAnim.GetCurrentAnimatorStateInfo(0).IsName("stillOpening"))
        {
            yield break;
        }
        counter = counter + Time.deltaTime;

        float val = Mathf.Lerp(currentVal, topSkyBoxOpen, counter / duration);

        skybox.SetFloat("_Exponent1", val);
        yield return null; //Wait for a frame
    }
    running = false;
}

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