KybernetikGames / animancer

Documentation for the Animancer Unity Plugin.
68 stars 8 forks source link

Proper way to pause/resume animation during transition #363

Closed jarrodspurrier closed 1 day ago

jarrodspurrier commented 1 month ago

In our project, we have a stun effect where we just want to pause all playing animations on the character. I've tried the following solutions each with their own issues:

Solution 1: AnimancerComponent.Playable.PauseGraph()/ResumeGraph() Problem 1: This doesn't pause the current animations in the way I would expect, as the character will just return to a T-Pose while the graph is paused. Is this intended, or is there some mis-configuration that might be contributing to this behavior?

Solution 2: Set AnimancerState.IsPlaying to false and AnimancerState.TargetWeight to AnimancerState.Weight (to all playing states on each layer) Problem 2: This will properly freeze the current playing animation, however when I go to resume it, if a layer was fading from one state to another, it will not resume the fade. Code 2:

    public void PauseAnimation()
    {
        AnimationPaused = true;

        PausedStates.Clear();

        foreach (var layer in Animancer.Layers)
        {
            foreach (var state in layer)
            {
                if (state.IsPlaying)
                {
                    PausedStates.Add(new PausedStateData(state, state.TargetWeight));

                    state.IsPlaying = false;
                    state.TargetWeight = state.Weight;
                }
            }
        }
    }

    public void UnPauseAnimation()
    {
        AnimationPaused = false;

        foreach (var stateData in PausedStates)
        {
            stateData.State.IsPlaying = true;
            stateData.State.TargetWeight = stateData.TargetWeight;
        }

        PausedStates.Clear();
    {

Any advice? I feel like this would be something that should be fairly easy to do with Animancer.

KybernetikGames commented 1 month ago

PauseGraph and ResumeGraph should be correct. That's what the Update Rate sample uses and I've never seen it return a character to their T pose so there must be something else causing that.

KybernetikGames commented 1 month ago

Have you made any progress on tracking down the cause of this issue?

jarrodspurrier commented 1 month ago

I was not able to get solution 1 to work. But I was able to get solution 2 to work by additionally caching the FadeSpeed when paused, then restoring it on the state when un-pausing.

KybernetikGames commented 1 month ago

Another option would be to set the AnimancerComponent.Playable.Speed = 0 which should affect everything, but I still can't see any reason for PauseGraph to not work. Are you using any other systems that might affect the animations like Animation Rigging or something?

jarrodspurrier commented 1 month ago

We are using both AnimationRigging and FinalIK. I don't have a whole lot of time to look into this right now, as we are moving really quickly. But I hope to revisit this in the next week or 2 and keep you updated.