KybernetikGames / animancer

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

Any example of creating 2d mixer state manually? #349

Closed drakeguo closed 3 months ago

drakeguo commented 3 months ago

I need to create a 2d Cartesian mixer that is equivalent to a blend tree in an animator controller in script. I tried to create a CartesianMixerState using code like this

        public CartesianMixerState CreateCartesianMixerState()
        {
            CartesianMixerState mixer = new CartesianMixerState();
            mixer.Speed = 1.0f;
            mixer.SetDebugName(stateName);
            foreach (var animStateDesc in animStateDescs)
            {
                mixer.Add(animStateDesc.motion, animStateDesc.position);
            }
            return mixer;
        }

then I tried to play this CartesianMixerState using AnimancerComponent.Play but it didn't play the animations as I expected. I can see this state in the inspector window of AnimancerComponent. But it seemed this state is not playing at all.

I saw there is a example of manually creating LinearMixerState and using it in the Documentation. I wonder if CartesianMixerState can be used in this way as well.

Thanks.

KybernetikGames commented 3 months ago

That code looks like it should work fine. What does the Inspector look like?

drakeguo commented 3 months ago

normally, if I play the blend tree in the AnimatorController, there is a green bar indicating that the animation or AnimatorController is being played. 1715253233073_7E15C47C-0148-4fa8-B67F-6E025FD6DE19

But when I try to play CartesianMixerState created in script, or MixerTransition2D created in inspector. there is no green bar, and the animation is not playing at all. 1715253132507_83177709-1358-47bb-8E66-23880268E5DC the character will fade into its T-Pose, without playing animation. then when you play another animation, it will fade into the new animation from this T-Pose. so it seems that the transition kind of works, but MixerState seems to be outputing a T-Pose instead of an animation.

KybernetikGames commented 3 months ago

Are you sure you're playing it and not just creating it? I can't think of any reason for that to not work, I have a bunch of unit tests which create mixers just like that.

If you can't figure it out, feel free to send a minimal reproduction project to animancer@kybernetik.com.au so I can take a look at it.

drakeguo commented 3 months ago

I found where the problem seems to be. Probably due to a bug in older version of Animancer. I have to call state?.RecreatePlayable() for ControllerState, otherwise the animation will not be played from its start. but if I called state?.RecreatePlayable() for CartesianMixerState it will freeze the animation. Not calling state?.RecreatePlayable() solved my problem.

Thank you very much for the information.