How can I get an animation's fadetime?

I’m currently having the character animations be imitated by a viewmodel.
While, I had managed to get that done, I don’t know if I could copy the fadeTime (i.e. animation:Play(fadeTime, …)) of the original animations into the duplicated ones as they are played. Anyone have any ideas? Seems like it’s only the weight that I can get hold of. Thanks.

1 Like

Why don’t you pass the fadeTime as an argument when communicating what animation to play?

1 Like
IEnumerator FadeAnimation (Animation originalAnim, Animation duplicateAnim, float fadeTime) 
{
    float timeElapsed = 0.0f;
    float originalWeight = originalAnim[originalAnim.clip.name].weight;
    float originalSpeed = originalAnim[originalAnim.clip.name].speed;
    float duplicateWeight = duplicateAnim[duplicateAnim.clip.name].weight;
    float duplicateSpeed = duplicateAnim[duplicateAnim.clip.name].speed;
    
    while (timeElapsed < fadeTime)
    {
        float weightDiff = Mathf.Abs(originalWeight - duplicateWeight);
        float speedDiff = Mathf.Abs(originalSpeed - duplicateSpeed);
        float weightStep = weightDiff * Time.deltaTime / fadeTime;
        float speedStep = speedDiff * Time.deltaTime / fadeTime;
        
        if (duplicateWeight < originalWeight)
        {
            duplicateWeight += weightStep;
        }
        if (duplicateSpeed < originalSpeed)
        {
            duplicateSpeed += speedStep;
        }
        
        timeElapsed += Time.deltaTime;
        yield return null;
    }
    
    duplicateAnim[originalAnim.clip.name].speed = originalSpeed;
    duplicateAnim.CrossFade(originalAnim.clip.name, fadeTime);
}

You could call this coroutine with the original animation, the duplicated animation, and the fade time like this:

StartCoroutine(FadeAnimation(originalAnim, duplicateAnim, fadeTime));
2 Likes