Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Crossfade to same clip

Discussion in 'Animation' started by petey, Mar 6, 2018.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hi all,

    I am trying to use animator.Crossfade to crossfade to the same clip, but I cant get it to work :(. It just crossfades to the finished clip and doesn't restart it.
    I can sorta understand why it wouldn't work, just wondering if anyone could think of a workaround? I had thought to maybe create two identical clips and crossfade to the second if the first is in use but I can't find a way to check if an animation is in use. (also that sounds a bit hacky)

    Any ideas?
    Thanks!
    Pete
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Can you explain a little more the expected outcome?
    What will you be cross fading from? The same clip you want to crossfade to?
    Is this to solve a problem you are experiencing?
     
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Well if you had say a punch animation that you wanted to be able to interrupt mid way through and play again. If you use animator.Crossfade to blend to that punch clip, it will only play once no matter how many times you call that.
    I could rewind it and play again but it would be nice to get a bit of blend.
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Sorry petey - you are working in code so I can't offer much help.
    I'd refer you to Exit time and Transition Duration - which allows a transition to happen (whenever) during a looping animation - and the duration controls how long the transition lasts.
    https://docs.unity3d.com/Manual/class-Transition.html
    This would work having two instances of the same clip in the state machine.
     
  5. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    looked up swapping/adding animation clips runtime

    and one way would be AnimatorOverrideController.
    https://docs.unity3d.com/Manual/AnimatorOverrideController.html

    which could change clips...
    I didn't tried it out yet, but might be able to have clips in a blend tree changed by an animator override controller
    and crossfading from a clip to a blend tree (setting blend variable to get the right clip).

    this way I think it'd be possible to have 9 different clips separately contained in a blend tree

    also this might be needed
    https://docs.unity3d.com/ScriptReference/AnimatorOverrideController.ApplyOverrides.html


    I guess making duplicate states is the easiest route
     
    Last edited: Mar 7, 2018
    theANMATOR2b likes this.
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Thanks guys, It seems like a simple idea but there’s no simple way to do it by the looks of things :(
    I really hope playables bring some animation improvements to Unity, I just find mecanim so frustrating.
     
  7. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Okay I'm back on this problem for one last desperate try.

    I came up with a solution where I duplicated the same clip and alternate between which one is playing. That worked, but I'm finding other clips that need to have the same behavior everywhere, so I'd have to potentially duplicate everything just incase (which is just getting silly).

    Next I was thinking, maybe a trigger that sends off a transition to itself -

    That worked too but, I'd essentially have to have a trigger for every clip, just incase it had to repeat.

    I feel like I'm going crazy o_O is it really such an unusual thing to want to play the same animation clip twice in row? I can't be the only one, seriously!

    Thanks,
    Pete
     
    ununion and miracletechteam1 like this.
  8. miracletechteam1

    miracletechteam1

    Joined:
    Sep 18, 2019
    Posts:
    24
    The simplest solution i have is using coroutine to wait next frame :' )

    Code (CSharp):
    1. if (Animator.GetCurrentAnimatorStateInfo(0).shortNameHash == yourAnimationHash)
    2. StartCoroutine(CrossFadeAnimAsync());
    3.  
    4. IEnumerator CrossFadeAnimAsync() {
    5. Animator.CrossFade(defaultAnimation, 0.1f, 0); ///Crossfade to DefaultAnimationName
    6. yield return new WaitForEndOfFrame();
    7. Animator.CrossFade(yourAnimation, 0.1f, 0); ///Crossfade to the real AnimationName
    8. }
    Yes this is not really good, but work as expected :D
     
    bobadi likes this.
  9. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,485
    That shouldn't work. If yourAnimation is still fading out (which it should be since you only waited one frame) then telling it to CrossFade back in will do nothing from what I remember. The only way I ever found to do this in an Animator Controller was to just make multiple states with the same clip and alternate between them as necessary.

    I implemented Fade Modes in Animancer to make that sort of thing easier by automatically creating extra states when they are needed.
     
  10. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    591
    CrossFadeInFixedTime can crossfade to same state.

    mystery why it's not in the docs.

    miracletechteam1
    this is some kinda crossfade-playback machine?
    it's funky it's heavy it's dope like hell. got my brain stuck on that like an ant on honey
     
    Last edited: Oct 8, 2020
  11. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Animancer seems like the tool I have been needing. I have one question tho since I have been playing with the demo. Does it supports Crossfading between the AnimationController and the played animations on an Hybrid setup? I have been looking at the hybrid sample scene and there is no cross fading on those.

    Thanks

    Update
    I think it works, just to confirm, is this the way to do it?
    Code (CSharp):
    1.  _Animancer.Play(_GolfSwing, 0.25f, FadeMode.FromStart).Events.OnEnd = () => _Animancer.PlayController();
     
    Last edited: Nov 3, 2020
  12. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,485
    Yes, PlayController will use the Fade Duration you specify in the HybridAnimancerComponent's Inspector, or you can do _Animancer.Play(_Animancer.Controller, fadeDuration) if you want to specify the duration in the script.
     
    Hazneliel likes this.
  13. inferno222

    inferno222

    Joined:
    Apr 29, 2014
    Posts:
    6
    If anyone is looking to implement crossfade with the legacy animation component, i've implemented it.
    What I did was the following:

    1. Check if the request animation is the same:
    Code (CSharp):
    1. string animationName = animationClip.name;
    2. if (animationComponent_.isPlaying && animationName_ == animationName)
    3. {
    4.     animationName += "_Clone"; // [AKP] Allow crossfade to same animation.
    5. }
    2. Add the clip ( Also validate the legacy field & add the clip to a list to clean it later, but that's not in the code )
    Code (CSharp):
    1. if (animationComponent_.GetClip(animationName) == null)
    2. {
    3.     animationComponent_.AddClip(animationClip, animationName);
    4. }
    5.  
    3. Set the clip and cache the name
    Code (CSharp):
    1. animationComponent_.clip = animationClip;
    2. animationName_ = animationName;
    4. Crossfade it
    Code (CSharp):
    1. animationComponent_.CrossFade(animationName, fadeLength: crossFadeLength_);
    I hope this helps somebody.
     
    petey likes this.
  14. cemugurk

    cemugurk

    Joined:
    Nov 27, 2014
    Posts:
    33
    thanks for reporting here, you saved my skill and animation pipeline.