Animation switching in Unity

In unity, the switching of 3D models can use the animator state machine or code switching Animator.CrossFade switching function

Switching with a state machine

It is necessary to connect the logical relationship of each animation, and add switching parameters, set the exit time, adjust the switching duration, etc. Then directly use the switching parameters such as SetTrigger to switch.

The advantage is that it is intuitive and easy to edit. The disadvantage is that there are too many connections, which is cumbersome.

Toggle using the API

The advantage of api switching is that you don't need to connect, you can use code switching directly, and it can achieve transition effects. The disadvantage is that it is not intuitive, and the transition parameters of each animation are different, which may need to be configured.

There are two APIs here, Animator.CrossFade and Animator.CrossFadeInFixedTime

The functions of the two APIs are the same, and the difference between Fixed is that the parameter is a fixed number of seconds; the time in CrossFade is set based on the normalized length of the animation clip.

public void CrossFade(string stateName, float normalizedTransitionDuration, int layer = -1, float normalizedTimeOffset = float.NegativeInfinity, float normalizedTransitionTime = 0.0f);

stateName: animation name. Switch to target animation clip

normalizedTransitionDuration: Switching duration. Switch to the target animation, the time required for the switching process

layer: layer. target animation layer

normalizedTimeOffset: time offset. Switch to the position of the target animation, for example, switch to 0.5 of the target state (50% of the animation)

normalizedTransitionTime: switching time. I don't know what this value specifically means. (Tested it, if it is close to 1, it will go directly to the target state without transition. If it is close to 0, there will be a transition, and other switching parameters will take effect. If you understand, please tell me)

Guess you like

Origin blog.csdn.net/Ling_SevoL_Y/article/details/129267744